how to read excel files by (column, row) with npoi and VISUAL BASIC - vb.net

I'd like to be able to ready an excel file by column index, row by row, in VB .NET.
I can do it extremely easily with python XLRD, and with vb6 via "ADOX.Catalog"
Basically, this should be enough for my needs:
wb = open(excelfile)
ws = wb.worksheet(0)
cell = ws.get_cell(col, row)
How can I do that?
Is there a way to get the columns of a worksheet?
I haven't found any documentation of NPOI
I haven't found any *.vb file samples in the NPOI downloads (alpha, beta and stable versions)
is there a unified excel file opener that will detect the file version and pick up the correct opener class, or do i have to do it by file extension?
subjective question: am I the only one who think this API is overly complex?
IMPORTANT EDIT: I CAN'T INSTALL EXCEL ON THE MACHINE, so i'm looking for non-excel solutions
ps. i'm new to .net

Not sure what npoi is, but this may help, assuming you have declared them:
xlObject = new excel.application()
xlWB = xlObject.workbooks(0) // I think it also takes string (name of wb)
sheet = xlWB.worksheet(0)
sheet.Range("B2") //specific cell
Are you talking about xls vs xlsx? It should handle both fine...
And NO! It's not nearly as intuitive or simple as it should be!

Related

using openpyxl to update certain columns in a complex spreadsheet

Hi I have a pretty complex spreadsheet and I need to update "ONLY" certain part of its contents.
I tried openpyxl, which works ok except all the pivot tables, styles and formulas are lost, after save the workbook. Is there a way to get around that?
Check which version of openpyxl you have installed. Support for Pivot Tables was only added in version 2.5.0-a1 (2017-05-30), and that only supported one Pivot Table until version 2.5.0-b2 (2018-01-19). See changelog.
Alternatives: Simplify, xlwings, content protection, and win32com (most likely).
If you are using 2.5 and it doesn't work, you'll probably have to simplify your spreadsheet if you want to continue to use openpyxl. E.g. split it into a source and output file, and use python to update the source file.
You could try running xlwings to host the Python code inside of the spreadsheet itself.
Another possible solution would be to lock the contents - turn on protection - for all except the sections you plan to update. This is unlikely to work as python is changing the contents directly, but it may respect the lock.
Finally, the result most likely to work. Drive the changes through the win32com module. I think this, unlike openpyxl, requires Excel to be installed as it is using Excel's functionality to make the changes. Examples of usage are here and here.
A brief example (taken from here):
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets("Sheet1")
ws.Cells(1,1).Value = "Cell A1"
ws.Cells(1,1).Offset(2,4).Value = "Cell D2"
ws.Range("A2").Value = "Cell A2"
ws.Range("A3:B4").Value = "A3:B4"
ws.Range("A6:B7,A9:B10").Value = "A6:B7,A9:B10"
wb.SaveAs('ranges_and_offsets.xlsx')
excel.Application.Quit()

Copy whole worksheet with openpyxl

Please can someone give me an example, how to copy
a whole worksheet with styles (from rows and columns)
to a second worksheet in the same workbook ?
(in a new workbook would also be possible)
Thank you.
P.S.: I tried to do a deepcopy, but that failed on saving changed data cells.
Purpose is: I try to fill some worksheets with my data and the first worksheet is my template.
I was successful in copying the values but only some styles.
I am using the latest version of openpyxl, so please no 1.x methods.
Version 2.4 will allow you to do this: copy_worksheet
>>> source = wb.active
>>> target = wb.copy_worksheet(source)
For older ones you can probably copy the source code from here
UPDATE: You can't simply graft this code into older versions of the library
You can't do this easily. The best approach is probably the one described in bug 171
I had the same problem. I solved using copy instead deepcopy. I found the solution on this site
I hope this works for you!
Regarding the links for the Openpyxl repository on #charlie-clark and #sam answers, these are not working anymore since the repo was moved from Bitbucket to Gitlab.
The updated link for issue 171 is https://foss.heptapod.net/openpyxl/openpyxl/-/issues/171
Notice that the copy_worksheet method does not copy data validations. You can do this manually using openpyxl's add_data_validation. A full working example:
# Copy worksheet
workbook = openpyxl.load_workbook(source_excel)
sheet_to_copy = workbook[sheet_name]
new_sheet = workbook.copy_worksheet(sheet_to_copy)
new_sheet.title = new_sheet_name
# Copy data validations
for data_validation in sheet_to_copy.data_validations.dataValidation:
new_sheet.add_data_validation(data_validation)
# Save result
workbook.save(source_excel)

save datagrid as excel 2003 or excel 2007 using vb.net

i can save a datagrid using the vb.net code:
saveFileDialog.Filter = "Excel2007 (*.xlsx)|*.xlsx|All files (*.*)|*.*"
i wish to give the user the option to save in either Excel2003 or Excel2007 format. What changes do i need to make in the code and what about references and imports? thanks...
Additionally to the adequate extension, you have to provide the Excel version when calling SaveAs():
curBook.SaveAs("full path with adequate extension", Excel.XlFileFormat.xlWorkbookDefault)
You can find the list of values for XlFileFormat here. Although the names in this list are not too descriptive. Here you have a translation:
2003 -> Excel.XlFileFormat.xlExcel8 (as explained here)
2007 -> Excel.XlFileFormat.xlOpenXMLWorkbook (as explained here)
NOTE: bear in mind that the code above relies on Office Interop.

Datasource or Linq Result to excel with VB.Net

i have found alot of sh*** right now. But not the things that im looking for.
I need to export a LINQ query (or the Datasource of my DataGridview)
to an existing Excel Spreadsheet. Lets Say after Cell A25
I have a Windows Form application using LINQ for the Databindings.
Any Suggestions or good Examples?
Thanks in Advance
I would recommend to use EPPlus because it's simple, powerful and works without having office/excel being installed with Excel 2007 spreadsheets(xlsx-files). It's license model is GPL.
Dim excel As New ExcelPackage
excel.File = New IO.FileInfo("C:\Temp\AnExcelFile.xlsx")
If excel.File.Exists Then excel.Load(excel.File.Open(FileMode.Open))
Dim ws As ExcelWorksheet = excel.Workbook.Worksheets.Add("Worksheet-Name")'must be unique and less than 31 characters long'
ws.Cells(26, 1).LoadFromDataTable(dt, True) 'loading from DataTable, the 2.Parameter is PrintHeaders'
ws.Cells(26, 1).LoadFromCollection(query, True)'loading by LINQ-Query'
excel.Save()
I don't think that you can use it for xls-files without problems. Have a look what Jan said: http://epplus.codeplex.com/discussions/253371
No, only XLSX, but you can open it in Excel 2003 if you have the
compatibility pack installed. If you need XLS, have a look at the
NPOI project here on Codeplex.

Combine several Excel files into one using VB

I have a folder with several excel files that have a date field, i.e. 08-24-2010-123320564.xls. I want to be able to have some VB scripting that will simply take the files that start with todays date and merge them into one file.
08-24-2010-123320564.xls
08-24-2010-123440735.xls
08-24-2010-131450342.xls
into
08-24-2010.xls
Can someone please help?
Thanks
GabrielVA
assuming you just want to append rows in simple spreadsheets, follow this logic:
Psuedocode
use an excel macro
(you could just as well automate excel from vb but why vbscript alone since you need excel anyway?)
have it to a dir listing (dir function)
dim a date_start variable init to "new"
dim a merged_spreadsheet as new doc default to nothing
loop thru result of dir
if date_start <> start of filename
if merged_spreadsheet is not nothing
save it
set it to nothing
store start of date (left mid function) in date_start
if merged_spreadsheet is nothing
make a new one
open the file from the dir command's loop
select all the data
copy it
go to first empty row in merged_spreadsheet
paste it
loop files
if merged_spreadsheet is not nothing
save it
If you're not happy with all those 'nothings', you can set a separate flag to keep track of whether you have a merged_spreadsheet or not. Think about what happens for just one file in a date, no files at all, etc.
Of course you will tear out your hair finding out how to automate those excel functions. The secret to turning 'hard' into 'pretty darn easy' is this:
macro recorder will reveal the automation commands
They are not intuitive. So record a macro. Then do things that you'll need to do in your code. Stop recording and look at the result.
For example:
* load/save files
* select only entered fields
* Select all
* copy / switch files / paste
* create new sheet
* click in various single cells and type (how to examine/set a cell's contents)
In summary-
(1) Know exactly the steps to what you're doing
(2) Use macro recorder to give away the secrets of the excel object model. Steal its secrets.
Really this won't be all that hard if you marry these two concepts cleverly. Since the macro will be vbscript (at least if you use office 97 ;-) you can probably run it from vbs or vb6 if you want. A hop to vb.net shouldn't be that hard either.
Aspose makes it pretty easy to work with excel-files in .NET http://www.aspose.com