[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2007-12-06
4
Medium Priority
?
786 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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