Solved

Excel Automation - Importing Excel File into System.Data.DataTable

Posted on 2007-12-06
4
779 Views
Last Modified: 2013-12-16
Hi

I have an application that converts files from particular formats (including CSV, XLS etc) into my required output format.

I have, up until now, used the OleDB approach to open these bad boys and convert into my required format.  All was working well.  Basically, whatever the input format, I get it into a System.Data.DataTable and then convert away.

My problem is that recently, some of the files to be converted are coming password protected so I'm having to move toward the Excel Automation.  So, I can open the files and grab the required worksheets etc, but can't seem to work out how to simply bung the data from my worksheet into a DataTable ready for converting!!

Excel.Application myXLS = new Excel.Application();
Excel.Workbook wbk = myXLS.Workbooks.Open(file.FilePath, 0, true, 5, "password", "password", _
true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
Excel.Sheets sheets = wbk.Worksheets;
Excel.Worksheet wksht = (Excel.Worksheet)sheets.get_Item(1);

System.Data.DataTable dt = wksht.something_surely?

Help!

Thanks
Simon
0
Comment
Question by:sbennetts
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:koolnurd
ID: 20425968
try this way

// Open XLSDocument
XLSDocument document = new XLSDocument("AdvancedReport.xls");
DataSet dataSet = new DataSet("AdvancedReport");
for (int i = 0; i < document.WorkBook.Worksheets.Count; i++)
{
Worksheet worksheet = document.WorkBook.Worksheets[i];
DataTable table = dataSet.Tables.Add(worksheet.Name);
for (int column = 0; column <= worksheet.LastColumn; column++)
{
table.Columns.Add(string.Format("Column{0}", column));
}
for (int row = 0; row <= worksheet.LastRow; row++)
{
object[] data = new object[worksheet.LastColumn + 1];
for (int column = 0; column <= worksheet.LastColumn; column++)
{
data[column] = worksheet.Cell(row, column).Value;
}
table.Rows.Add(data);
}
}
// Close XLSDocument
document.Close();
0
 
LVL 2

Author Comment

by:sbennetts
ID: 20427046
Hi, thanks for your response.

No joy I'm afraid.  Falls over in several places, for instance there not being a lastcolumn or lastrow available.  Also, the worksheet.Cell seems to fall over as well.

I'm using MS Office v9.0 Object Library

Regards
Simon
0
 
LVL 2

Accepted Solution

by:
koolnurd earned 200 total points
ID: 20439532
ok...

use columns and rows it will give you the range...

And for selecting cell value...use like this...

((Excel.Range)worksheet.Cells[row, column]).Value2
0
 
LVL 2

Author Comment

by:sbennetts
ID: 20448293
Hi, thanks for your help, managed to do it more or less as you suggest.  Some subtle tweaks but very helpful.
Cheers
Simon
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question