?
Solved

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

Posted on 2007-12-06
4
Medium Priority
?
784 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

741 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