DataGridViewS export to excel sheetS - c#

I want to export all my DataGridViews in one Excell document.
For every DataGridView there shoud be a own sheet in the Excell File.
But with my code i only recive the Error: System.Runtime.InteropServices.COMException: HRESULT: 0x800A03EC"
I think there is something wrong with my parameters.
private void exportToExcellButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileD = new SaveFileDialog();
string fileName = truckListBox.SelectedItem.ToString() + "__" + DateTime.Now.ToShortDateString();
saveFileD.InitialDirectory = #"C:/TML/";
saveFileD.FileName = fileName;
if (!Directory.Exists(#"C:/TML/"))
Directory.CreateDirectory(#"C:/TML/");
List<DataGridView> dataGridViews = getAllDataGridViews();
Microsoft.Office.Interop.Excel.Application app;
Microsoft.Office.Interop.Excel.Workbook book;
Microsoft.Office.Interop.Excel.Worksheet sheet;
app = new Excel.Application();
app.Visible = true;
book = app.Workbooks.Add(System.Reflection.Missing.Value);
foreach (var grid in dataGridViews)
{
int count = book.Worksheets.Count;
sheet = (Worksheet)book.Sheets.Add(Type.Missing, book.Worksheets[count], Type.Missing, Type.Missing);
sheet.Name = grid.Name.ToString().Remove(0, 13);
int cMin = 0, rMin = 0;
int c = cMin, r = rMin;
// Set Headers
foreach (DataGridViewColumn column in grid.Columns)
{
//Here appears the Error: System.Runtime.InteropServices.COMException: HRESULT: 0x800A03EC"
sheet.Cells[r, c] = column.HeaderText;
c++;
}
sheet.Range[sheet.Cells[r, cMin], sheet.Cells[r, c]].Font.Bold = true;
sheet.Range[sheet.Cells[r, cMin], sheet.Cells[r, c]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
// Set Rows
foreach (DataGridViewRow row in grid.Rows)
{
r++;
c = cMin;
// Set Cells
foreach (DataGridViewCell item in row.Cells)
{
sheet.Cells[r, c++] = item.Value;
}
}
}
book.Save();
book.Close();
app.Quit();
}
Spended allready days into it and cant get it work.
Thx for your Help!
EDIT: Fixed one error to get to the new one.

There are a few problems you may have with the posted code. Therefore, I will break them down.
For starters, it appears you are using a SaveFileDialog however I do not see where it is being used. The code sets the InitalDirectory and FileName, but it is never used. This is not that important as a dialog is not really needed, however the way the code is getting the file name is going to have some problems. The line of code…
string fileName = truckListBox.SelectedItem.ToString() + "__" + DateTime.Now.ToShortDateString();
is going to have problems if you try to save the file name because the string returned from DateTime.Now.ToShortDateString() is going to be in a format like “2019\11\26”… Obviously the “\” characters are going to be interpreted as a path (folder) and will most likely fail when the code tries to save the file. Creating a method that returns a string that uses some other character should be easy to fix this.
Next is the fact that Excel files are NOT zero based on their rows and columns. Therefore, setting the initial Excel row column variables (int c = 0, r = 0;) will fail on the first try. These values should be one (1).
Another problem is on the line…
book.Save();
Is most likely going to save the file to the users “Documents” folder using the file name of “Book1.xlsx.” When saving the file you need to supply the complete path and file name which as stated earlier does not appear to be used.
Lastly, anytime you use “COM” objects such as Excel apps, workbooks and worksheets, it is very important for the code to “RELEASE” the com objects your code creates before you exit the program. In the current posted code, it is highly likely that there are lingering “Excel” resources still running. Therefore, to avoid leaking resources, it is important for your code to release the com objects it creates.
In my example below the code to release the resources is in the finally clause of the try/catch/finally statement.
private void button1_Click(object sender, EventArgs e) {
//SaveFileDialog saveFileD = new SaveFileDialog();
//string fileName = truckListBox.SelectedItem.ToString() + "__" + DateTime.Now.ToShortDateString();
string fileName = #"C:\Users\John\Desktop\Grr\TestExcelFile" + "__" + DateTime.Now.Year + "_" + DateTime.Now.Month;
//saveFileD.InitialDirectory = #"C:\Users\John\Desktop\Grr\";
//saveFileD.FileName = fileName;
//if (!Directory.Exists(#"C:/TML/"))
// Directory.CreateDirectory(#"C:/TML/");
//List<DataGridView> dataGridViews = getAllDataGridViews();
List<DataGridView> dataGridViews = getGrids();
Microsoft.Office.Interop.Excel.Application app = null;
Microsoft.Office.Interop.Excel.Workbook book = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = true;
book = app.Workbooks.Add(System.Reflection.Missing.Value);
try {
foreach (var grid in dataGridViews) {
int count = book.Worksheets.Count;
//sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets.Add(Type.Missing, book.Worksheets[count], Type.Missing, Type.Missing);
sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Worksheets.Add();
//sheet.Name = grid.Name.ToString().Remove(0, 13);
sheet.Name = grid.Name.ToString();
int cMin = 1, rMin = 1;
int c = cMin, r = rMin;
// Set Headers
foreach (DataGridViewColumn column in grid.Columns) {
//Here appears the Error: System.Runtime.InteropServices.COMException: HRESULT: 0x800A03EC"
sheet.Cells[r, c] = column.HeaderText;
c++;
}
sheet.Range[sheet.Cells[r, cMin], sheet.Cells[r, c]].Font.Bold = true;
sheet.Range[sheet.Cells[r, cMin], sheet.Cells[r, c]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
// Set Rows
foreach (DataGridViewRow row in grid.Rows) {
r++;
c = cMin;
// Set Cells
foreach (DataGridViewCell item in row.Cells) {
sheet.Cells[r, c++] = item.Value;
}
}
}
book.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing);
book.Close();
app.Quit();
}
catch (Exception ex) {
MessageBox.Show("Error writing to excel: " + ex.Message);
}
finally {
if (sheet != null)
Marshal.ReleaseComObject(sheet);
if (book != null)
Marshal.ReleaseComObject(book);
if (app != null)
Marshal.ReleaseComObject(app);
}
}
Hope this helps.

Simply Make a method and pass DataGridView
using Excel = Microsoft.Office.Interop.Excel;
public void ete(DataGridView dgv)//ExportToExcel
{
// Creating a Excel object.
Excel._Application excel = new Excel.Application();
Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
Excel._Worksheet worksheet = null;
excel.Columns.ColumnWidth = 20;
try
{
worksheet = workbook.ActiveSheet;
worksheet.Name = "ExportedFromDatGrid";
int cellRowIndex = 1;
int cellColumnIndex = 1;
//Loop through each row and read value from each column.
for (int i = -1; i < dgv.Rows.Count; i++)
{
for (int j = 0; j < dgv.Columns.Count; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
if (cellRowIndex == 1)
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = dgv.Columns[j].HeaderText;
}
else
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = dgv.Rows[i].Cells[j].Value.ToString();
}
cellColumnIndex++;
}
cellColumnIndex = 1;
cellRowIndex++;
}
//Getting the location and file name of the excel to save from user.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveDialog.FilterIndex = 2;
if (saveDialog.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName.ToString());
MessageBox.Show("Export Successful");
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit();
workbook = null;
excel = null;
}
}
Now Call Method
ete(datagridview1);

Related

How to append existing excel file using C#

Below are my code, I'm stuck with can't append the contents in the excel,
When first time load the program, the excel can output normal
But at Second time load the program, the excel will crash badly(can't open).
I'm using the FileMode.Append, FileAccess.Write, but still can't works
Is there have some step I missed it? please help me to solve this issue, thanks a lot!
XSSFWorkbook XSSFworkbook = new XSSFWorkbook(); //建立活頁簿
ISheet sheet = XSSFworkbook.CreateSheet(tbx_Build.Text); //建立sheet
//設定樣式
ICellStyle headerStyle = XSSFworkbook.CreateCellStyle();
IFont headerfont = XSSFworkbook.CreateFont();
headerStyle.Alignment = HorizontalAlignment.Center; //水平置中
headerStyle.VerticalAlignment = VerticalAlignment.Center; //垂直置中
headerfont.FontName = "Segoe UI";
headerfont.FontHeightInPoints = 12;
headerfont.Boldweight = (short)FontBoldWeight.Bold;
headerStyle.SetFont(headerfont);
XSSFCellStyle cs = (XSSFCellStyle)XSSFworkbook.CreateCellStyle();
cs.WrapText = true; // 設定換行
cs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Top;
//新增標題列
IRow headerrow = sheet.CreateRow(0);//建立行
headerrow.HeightInPoints = 20;
headerrow.CreateCell(0).SetCellValue("System_name");
headerrow.CreateCell(1).SetCellValue("Fixture_name");
headerrow.CreateCell(2).SetCellValue("build_ID");
headerrow.CreateCell(3).SetCellValue("start_time");
headerrow.CreateCell(4).SetCellValue("end_time");
headerrow.CreateCell(5).SetCellValue("serial_number");
headerrow.CreateCell(6).SetCellValue("Status");
headerrow.CreateCell(7).SetCellValue("Symptom_label");
headerrow.CreateCell(8).SetCellValue("Repair");
headerrow.CreateCell(9).SetCellValue("Measurement");
headerrow.CreateCell(10).SetCellValue("Board_slot");
headerrow.CreateCell(11).SetCellValue("Error");
headerrow.CreateCell(12).SetCellValue("Version");
for (int i = 0; i < 13; i++)
{
headerrow.GetCell(i).CellStyle = headerStyle; //套用樣式
}
//填入資料
int rowIndex = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row = sheet.CreateRow(rowIndex);//建立行
row.HeightInPoints = 18;
row.CreateCell(0).SetCellValue(Convert.ToString(dt.Rows[i]["System_name"]));
row.CreateCell(1).SetCellValue(Convert.ToString(dt.Rows[i]["Fixture_name"]));
row.CreateCell(2).SetCellValue(Convert.ToString(dt.Rows[i]["build_ID"]));
row.CreateCell(3).SetCellValue(Convert.ToString(dt.Rows[i]["start_time"]));
row.CreateCell(4).SetCellValue(Convert.ToString(dt.Rows[i]["end_time"]));
row.CreateCell(5).SetCellValue(Convert.ToString(dt.Rows[i]["serial_number"]));
row.CreateCell(6).SetCellValue(Convert.ToString(dt.Rows[i]["Status"]));
row.CreateCell(7).SetCellValue(Convert.ToString(dt.Rows[i]["Symptom_label"]));
row.CreateCell(8).SetCellValue(Convert.ToString(dt.Rows[i]["Repair"]));
row.CreateCell(9).SetCellValue(Convert.ToString(dt.Rows[i]["Measurement"]));
row.CreateCell(10).SetCellValue(Convert.ToString(dt.Rows[i]["Board_slot"]));
row.CreateCell(11).SetCellValue(Convert.ToString(dt.Rows[i]["Error"]));
row.CreateCell(12).SetCellValue(Convert.ToString(dt.Rows[i]["Version"]));
if (dt.Rows[i]["Error"].ToString().Contains("\n"))
{
row.GetCell(12).CellStyle = cs;
row.HeightInPoints = 45;
}
else if (dt.Rows[i]["Repair"].ToString().Contains("\n"))
{
row.GetCell(12).CellStyle = cs;
row.HeightInPoints = 45;
}
sheet.AutoSizeColumn(i); //欄位自動調整大小
rowIndex++;
}
string newName2 = "Yield.xlsx";
string exportpath2 = server_backup_failed + "\\output";
if (!System.IO.Directory.Exists(exportpath2))
{
System.IO.Directory.CreateDirectory(exportpath2);//不存在就建立目錄
}
var file2 = new FileStream(exportpath2 + "\\" + newName2, FileMode.Append, FileAccess.Write);
XSSFworkbook.Write(file2, true);
file2.Close();
XSSFworkbook.Close();
}
As already commented… I can confirm from a few tests that …
FileMode.Append
Will not work as you are intending. This seems somewhat obvious since a workbook can have multiple worksheets and what you describe is appending the data to an existing worksheet… The FileStream will not know how to do this.
Therefore, as already described, you need to open the file, make the changes (append the rows) and then save the file and close it. In my tests this worked using most of your code with some minor changes. In your current code, it is always creating a “new” Excel workbook and worksheet. So if you want to “append” something to a worksheet, then that “implies” that the workbook and worksheet may already exists. So, if the workbook already exists, then we want to open it and not create a new one.
Therefore, I suggest a small method that takes a string file path and file name and returns a workbook. If the workbook exists, then we simply return the workbook. If the workbook does not exist, then we return a “new” workbook. If the path is bad or any other failure… we will return a null value. Something like…
private XSSFWorkbook GetExcelWorkbook(string filePath) {
if (!File.Exists(filePath)) {
return new XSSFWorkbook();
}
try {
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
return new XSSFWorkbook(fs);
}
}
catch (Exception ex) {
Debug.WriteLine("Excel Error getting workbook: " + ex.Message);
return null;
}
}
This should help getting the workbook. Next, It appears the code is looking for a “specific” worksheet. Similar to the code above a simple method that takes a workbook and a worksheet name and returns a worksheet with the given name may come in handy. Again, if the worksheet already exists, then we simply return that worksheet. If the worksheet does not exist, then we will create a new worksheet with the given name. In addition, we will go ahead and add the headers row if the worksheet is new. This is also in another method so that you could also add the header row each time new data is appended. However in this example, the header is added only when a new worksheet is created and assumes the header row already exist in existing files.
This method may look something like…
private ISheet GetWorksheet(XSSFWorkbook wb, string targetSheetName) {
try {
for (int i = 0; i < wb.NumberOfSheets; i++) {
if (wb.GetSheetAt(i).SheetName.Equals(targetSheetName)) {
return wb.GetSheetAt(i);
}
}
ISheet ws = wb.CreateSheet(targetSheetName);
AddHeaders(wb, ws);
return ws;
}
catch (Exception e) {
Debug.WriteLine("Excel Error getting worksheet: " + e.Message);
return null;
}
}
A walkthrough of the code above is straight forward… Loop through the given workbooks worksheets to look for the target worksheet name. If the worksheet is found it is returned. If the worksheet is not found, then a new worksheet is created with the target name, add the headers to the new worksheet then return the new worksheet. If some error arises a null value is returned.
Next is the method that adds the headers to the worksheet and is taken almost directly from your code.
private void AddHeaders(XSSFWorkbook wb, ISheet sheet) {
IRow headerrow = sheet.CreateRow(0);
headerrow.HeightInPoints = 20;
headerrow.CreateCell(0).SetCellValue("System_name");
headerrow.CreateCell(1).SetCellValue("Fixture_name");
headerrow.CreateCell(2).SetCellValue("build_ID");
headerrow.CreateCell(3).SetCellValue("start_time");
headerrow.CreateCell(4).SetCellValue("end_time");
headerrow.CreateCell(5).SetCellValue("serial_number");
headerrow.CreateCell(6).SetCellValue("Status");
headerrow.CreateCell(7).SetCellValue("Symptom_label");
headerrow.CreateCell(8).SetCellValue("Repair");
headerrow.CreateCell(9).SetCellValue("Measurement");
headerrow.CreateCell(10).SetCellValue("Board_slot");
headerrow.CreateCell(11).SetCellValue("Error");
headerrow.CreateCell(12).SetCellValue("Version");
ICellStyle headerStyle = wb.CreateCellStyle();
IFont headerfont = wb.CreateFont();
headerStyle.Alignment = HorizontalAlignment.Center;
headerStyle.VerticalAlignment = VerticalAlignment.Center;
headerfont.FontName = "Segoe UI";
headerfont.FontHeightInPoints = 12;
headerfont.Boldweight = (short)FontBoldWeight.Bold;
headerStyle.SetFont(headerfont);
for (int i = 0; i < 13; i++) {
headerrow.GetCell(i).CellStyle = headerStyle;
}
}
These methods should simplify “appending” data to an existing worksheet or creating a new worksheet. So, using the above methods, the code below works as described here… This code is added to a button clicks event. Initially, some test data is created for the DataTable dt that will be appended to the worksheet. Next, we check if the given folder path exists and if not, then create it.
Next we use our methods above to get the workbook and also the worksheet. The worksheet name is as you have in the current code and is coming from a TextBox ...tbx_Build on the form.
Next, instead of setting rowIndex to 1, we set it to the last row of existing data in the worksheet so we can “append” the data. Specifically the line…
int rowIndex = ws.LastRowNum + 1;
Next a cell style is created and a loop through the DataTables dt rows where each cell is added to the worksheet. Note I removed the unnecessary “Convert” code and used the cells ToString() method. And finally a style is added to some cells for some reason.
And lastly, the FilesStream is created to save the file and possibly overwrite it when it’s not new.
You may note, that the workbook wb is closed in the finally portion of the try/catch/finally statement and the reason for this is that if the code fails sometime after the workbook is open, then it may not get closed. This ensures that the workbook is properly closed.
private void button1_Click(object sender, EventArgs e) {
string workbookName = "__NewBook_1.xlsx";
string saveFilePath = #"D:\Test\Excel_Test";
DataTable dt = GetTable();
FillTable(dt);
XSSFWorkbook wb = null;
try {
if (!Directory.Exists(saveFilePath)) {
Directory.CreateDirectory(saveFilePath);
}
wb = GetExcelWorkbook(saveFilePath + #"\" + workbookName);
if (wb != null) {
ISheet ws = GetWorksheet(wb, tbx_Build.Text);
if (ws != null) {
int rowIndex = ws.LastRowNum + 1;
ICellStyle cs = wb.CreateCellStyle();
cs.WrapText = true;
cs.VerticalAlignment = VerticalAlignment.Top;
for (int i = 0; i < dt.Rows.Count; i++) {
IRow row = ws.CreateRow(rowIndex);
row.HeightInPoints = 18;
row.CreateCell(0).SetCellValue(dt.Rows[i]["System_name"].ToString());
row.CreateCell(1).SetCellValue(dt.Rows[i]["Fixture_name"].ToString());
row.CreateCell(2).SetCellValue(dt.Rows[i]["build_ID"].ToString());
row.CreateCell(3).SetCellValue(dt.Rows[i]["start_time"].ToString());
row.CreateCell(4).SetCellValue(dt.Rows[i]["end_time"].ToString());
row.CreateCell(5).SetCellValue(dt.Rows[i]["serial_number"].ToString());
row.CreateCell(6).SetCellValue(dt.Rows[i]["Status"].ToString());
row.CreateCell(7).SetCellValue(dt.Rows[i]["Symptom_label"].ToString());
row.CreateCell(8).SetCellValue(dt.Rows[i]["Repair"].ToString());
row.CreateCell(9).SetCellValue(dt.Rows[i]["Measurement"].ToString());
row.CreateCell(10).SetCellValue(dt.Rows[i]["Board_slot"].ToString());
row.CreateCell(11).SetCellValue(dt.Rows[i]["Error"].ToString());
row.CreateCell(12).SetCellValue(dt.Rows[i]["Version"].ToString());
if (dt.Rows[i]["Error"].ToString().Contains("\n")) {
row.GetCell(12).CellStyle = cs;
row.HeightInPoints = 45;
}
else if (dt.Rows[i]["Repair"].ToString().Contains("\n")) {
row.GetCell(12).CellStyle = cs;
row.HeightInPoints = 45;
}
ws.AutoSizeColumn(i);
rowIndex++;
}
using (FileStream fs = new FileStream(saveFilePath + #"\" + workbookName, FileMode.Create, FileAccess.Write)) {
wb.Write(fs, true);
}
}
}
}
catch (Exception ex) {
Debug.WriteLine("Excel Error: " + ex.Message);
}
finally {
if (wb != null) {
wb.Close();
}
}
}
And finally the code that creates some test data to test the above methods and complete the example.
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using HorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment;
private DataTable GetTable() {
DataTable dt = new DataTable();
dt.Columns.Add("System_name");
dt.Columns.Add("Fixture_name");
dt.Columns.Add("build_ID");
dt.Columns.Add("start_time");
dt.Columns.Add("end_time");
dt.Columns.Add("serial_number");
dt.Columns.Add("Status");
dt.Columns.Add("Symptom_label");
dt.Columns.Add("Repair");
dt.Columns.Add("Measurement");
dt.Columns.Add("Board_slot");
dt.Columns.Add("Error");
dt.Columns.Add("Version");
return dt;
}
private void FillTable(DataTable dt) {
for (int i = 0; i < 20; i++) {
dt.Rows.Add("C0R" + (i + 1), "C1R" + (i + 1), "C2R" + (i + 1), "C3R" + (i + 1), "C4R" + (i + 1),
"C5R" + (i + 1), "C6R" + (i + 1), "C7R" + (i + 1), "C8R" + (i + 1), "C9R" + (i + 1),
"C10R" + (i + 1), "C11R" + (i + 1), "C12R" + (i + 1));
}
}
I hope this makes sense. I tested this numerous times and it appears to work as expected, however there may still be some necessary exception checking that was not added for brevity. If something is not correct please let me know and I will correct it if possible.

C# How to write each table in a Word file to its own Excel file

I'm trying to write code in C# WinForms that allows a user to select a directory tree, and extract all of the table data from a word document into an excel file. Presently, the code compiles and you can select your directories, etc, but once it begins to iterate through the loop for each table it crashes.
The program successfully opens the first word file and writes the first excel file (table_1_whatever.xlsx) and saves it in the destination folder. However, on the second table in the same file I get this error on this line of code:
worksheet.Cells[row, col] = objExcelApp.WorksheetFunction.Clean(table.Cell(row, col).Range.Text);
System.Runtime.InteropServices.COMException: 'The requested member of the collection does not exist.'
I can't seem to figure out why it doesn't exist. Each time it goes through the foreach loop it should be creating a new worksheet, but it doesn't appear to be working. Any insight, examples, or suggestions are welcome!
Code:
private void WordRunButton_Click(object sender, EventArgs e)
{
var excelApp = new excel.Application();
excel.Workbooks workbooks = excelApp.Workbooks;
var wordApp = new word.Application();
word.Documents documents = wordApp.Documents;
wordApp.Visible = false;
excelApp.Visible = false;
string[] fileDirectories = Directory.GetFiles(WordSourceBox.Text, "*.doc*",
SearchOption.AllDirectories);
foreach (var item in fileDirectories)
{
word._Document document = documents.Open(item);
int tableCount = 1;
foreach (word.Table table in document.Tables)
{
if (table.Cell(1, 1).ToString() != "Doc Level")
{
string wordFile = item;
appendName = Path.GetFileNameWithoutExtension(wordFile) + "_Table_" + tableCount + ".xlsx";
var workbook = excelApp.Workbooks.Add(1);
excel._Worksheet worksheet = (excel.Worksheet)workbook.Sheets[1];
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 1; col <= table.Columns.Count; col++)
{
var cell = table.Cell(row, col);
var range = cell.Range;
var text = range.Text;
var cleaned = excelApp.WorksheetFunction.Clean(text);
worksheet.Cells[row, col] = cleaned;
}
}
workbook.SaveAs(Path.Combine(WordOutputBox.Text, Path.GetFileName(appendName)), excel.XlFileFormat.xlWorkbookDefault);
workbook.Close();
Marshal.ReleaseComObject(workbook);
}
else
{
WordOutputStreamBox.AppendText(String.Format("Table {0} ignored\n", tableCount));
}
WordOutputStreamBox.AppendText(appendName + "\n");
tableCount++;
}
document.Close();
Marshal.ReleaseComObject(document);
WordOutputStreamBox.AppendText(item + "\n");
}
WordOutputStreamBox.AppendText("\nAll files parsed");
excelApp.Application.Quit();
workbooks.Close();
excelApp.Quit();
WordOutputStreamBox.AppendText("\nExcel files closed");
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excelApp);
WordOutputStreamBox.AppendText("\nExcel files released");
wordApp.Application.Quit();
wordApp.Quit();
WordOutputStreamBox.AppendText("\nWord files have been quit");
Marshal.ReleaseComObject(documents);
Marshal.ReleaseComObject(wordApp);
WordOutputStreamBox.AppendText("\nWord files have been released\n");
}
Edit 1:(Sorry for posting in the wrong place the first time!)
Ok, so the problem has been isolated...
The code logic of the code was fine, and the table was in fact there. The issue is that the second table of these files has a set of split cells in it, so, when it reaches the cell that contains it, the program crashes.
As a temp fix, I have just set it to ignore the table if the header == whatever. Does anyone know of a solution that actually allows to extract this data though?

COM Exception from HRESULT: 0x800A03EC on Excel Save File

This has been bothering me for some time. I am trying to export a report to an excel function in a program. Everytime when I export the report and it prompts me to name and save the file, it throws this error :
COM Exception from HRESULT: 0x800A03EC
I know this has been asked in many forums but those solutions aren't working at all for me. I tried restarting VS, run VS as admin, changing column rows numbers,etc. No luck at all. The thing is, I have 10 different functions in the program and like 4-5 of them allowed me to save and export to excel successfully but the others functions throw this error.
This is part of the code that throws this error :
private void ExportExcel(SqlDataReader dr) {
try {
DataTable dt = new DataTable();
dt.Load(dr);
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Microsoft Office Excel Workbook (*.xls)|*.xls|All Files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
// Create an Excel object and add workbook...
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true); // true for object template???
// Add column headings...
int iCol = 0;
int iVisibleColumnCount = 0;
foreach (DataColumn c in dt.Columns) {
iCol++;
// counting visible columns
if (c.ColumnMapping != MappingType.Hidden)
iVisibleColumnCount++;
else // hide the columns in excel is the column is hide in datatable
{
((Excel.Range) excel.Cells[1, iCol]).EntireColumn.Hidden = true;
continue;
}
// Set column header text to bold
((Excel.Range) excel.Cells[1, iCol]).Font.Bold = true;
excel.Cells[1, iCol] = c.ColumnName;
if (c.DataType == typeof(System.String))
((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "#";
else if (c.DataType == typeof(System.Int16)
|| c.DataType == typeof(System.Int32)
|| c.DataType == typeof(System.Int64))
((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "#,##0";
else if (c.DataType == typeof(System.TimeSpan))
((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = #"[$-409]hh:mm:ss AM/PM;#";
else if (c.DataType == typeof(System.DateTime))
((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "dd-MMM-yyyy";
else if (c.DataType == typeof(System.Decimal))
((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = #"#,##0.00_);[Red](#,##0.00)";
else
((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "General";
}
// for each row of data...
int iRow = 0;
foreach (DataRow r in dt.Rows) {
iRow++;
// add each row's cell data...
iCol = 0;
foreach (DataColumn c in dt.Columns) {
iCol++;
if (c.ColumnMapping != MappingType.Hidden) {
Exception thrown at this line: excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
}
}
}
// // Global missing reference for objects we are not defining...
// object missing = System.Reflection.Missing.Value;
// If wanting to Save the workbook...
workbook.SaveAs(saveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// workbook.SaveAs(saveFileDialog1.FileName,
// Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
// false, false, Excel.XlSaveAsAccessMode.xlNoChange,
// missing, missing, missing, missing, missing);
// If wanting to make Excel visible and activate the worksheet...
//excel.Visible = !bCloseAfterExport;
Excel.Worksheet xlWorkSheet = (Excel.Worksheet) excel.ActiveSheet;
((Excel._Worksheet) xlWorkSheet).Activate();
workbook.Save();
//save the workbook
//if (bCloseAfterExport)
workbook.Close(false, String.Empty, false);
//close the workbook
// End
// If wanting excel to shutdown...
//if (bCloseAfterExport)
((Excel._Application) excel).Quit();
}
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
Error message:
Any help would be appreciated.
I believe my issue was some of my data (cell values) started with "=" and that caused the assignment to fail. Difficult to see when you're doing 000s of rows at once. Put the transfer in a loop and transfer 10 rows at a time (in a try catch block too so it can carry on).
excel.Cells[iRow + 1, iCol] is a range. It has a Range type. you cannot set values like this. To set a value for a Range, you need to set its value or formula:
excel.Cells[iRow + 1, iCol].Value = r[c.ColumnName];

open and edit multiple .csv files in c#

My ideal is : open some .csv files (5 or 6 or more) and add 2 new columns to all opened files and finally save it. Here is my code
OpenFileDialog fopen = new OpenFileDialog();
fopen.Multiselect = true;
fopen.Filter = "(All type)|*.*";
fopen.ShowDialog();
if (fopen.FileName != null)
{
Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Open(fopen.FileName);
Excel.Worksheet sheet = wb.Sheets[1];
Excel.Range range = sheet.UsedRange;
int column = range.Columns.Count;
int row = range.Rows.Count;
textBox1.Text = fopen.FileName;
//textBox2.Text = row.ToString();
//textBox3.Text = column.ToString();
range.Cells.set_Item(1, column + 1, "Mo_stMoC");
range.Cells.set_Item(1, column + 2, "Mo_stMoCCpl");
for (int i = 2; i <= row; i++)
{
range.Cells.set_Item(i, column + 1, "0");
range.Cells.set_Item(i, column + 2, "0");
}
wb.Save();
wb.Close();
app.Workbooks.Close();
app.Quit();
}
The problem is, when I open the files and it just adding 2 columns in the first .csv file.
I'm new to C#, so what am I doing wrong here?
you are opening multiple files, so loop through fopen.FileNames instead of just using fopen.FileName
foreach (String file in fopen.FileNames)
{
//do your thing
//edit
}
Additionally it is better to filter only csv files instead of all.
fopen.Filter = "CSV Files (*.csv)|*.csv";

How to read large excel file in asp.net c# by splitting it into simpler? [duplicate]

How to read an Excel file using C#? I open an Excel file for reading and copy it to clipboard to search email format, but I don't know how to do it.
FileInfo finfo;
Excel.ApplicationClass ExcelObj = new Excel.ApplicationClass();
ExcelObj.Visible = false;
Excel.Workbook theWorkbook;
Excel.Worksheet worksheet;
if (listView1.Items.Count > 0)
{
foreach (ListViewItem s in listView1.Items)
{
finfo = new FileInfo(s.Text);
if (finfo.Extension == ".xls" || finfo.Extension == ".xlsx" || finfo.Extension == ".xlt" || finfo.Extension == ".xlsm" || finfo.Extension == ".csv")
{
theWorkbook = ExcelObj.Workbooks.Open(s.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
for (int count = 1; count <= theWorkbook.Sheets.Count; count++)
{
worksheet = (Excel.Worksheet)theWorkbook.Worksheets.get_Item(count);
worksheet.Activate();
worksheet.Visible = false;
worksheet.UsedRange.Cells.Select();
}
}
}
}
OK,
One of the more difficult concepts to grasp about Excel VSTO programming is that you don't refer to cells like an array, Worksheet[0][0] won't give you cell A1, it will error out on you. Even when you type into A1 when Excel is open, you are actually entering data into Range A1. Therefore you refer to cells as Named Ranges. Here's an example:
Excel.Worksheet sheet = workbook.Sheets["Sheet1"] as Excel.Worksheet;
Excel.Range range = sheet.get_Range("A1", Missing.Value)
You can now literally type:
range.Text // this will give you the text the user sees
range.Value2 // this will give you the actual value stored by Excel (without rounding)
If you want to do something like this:
Excel.Range range = sheet.get_Range("A1:A5", Missing.Value)
if (range1 != null)
foreach (Excel.Range r in range1)
{
string user = r.Text
string value = r.Value2
}
There might be a better way, but this has worked for me.
The reason you need to use Value2 and not Value is because the Value property is a parametrized and C# doesn't support them yet.
As for the cleanup code, i will post that when i get to work tomorrow, i don't have the code with me, but it's very boilerplate. You just close and release the objects in the reverse order you created them. You can't use a Using() block because the Excel.Application or Excel.Workbook doesn't implement IDisposable, and if you don't clean-up, you will be left with a hanging Excel objects in memory.
Note:
If you don't set the Visibility property Excel doesn't display, which can be disconcerting to your users, but if you want to just rip the data out, that is probably good enough
You could OleDb, that will work too.
I hope that gets you started, let me know if you need further clarification. I'll post a complete
here is a complete sample:
using System;
using System.IO;
using System.Reflection;
using NUnit.Framework;
using ExcelTools = Ms.Office;
using Excel = Microsoft.Office.Interop.Excel;
namespace Tests
{
[TestFixture]
public class ExcelSingle
{
[Test]
public void ProcessWorkbook()
{
string file = #"C:\Users\Chris\Desktop\TestSheet.xls";
Console.WriteLine(file);
Excel.Application excel = null;
Excel.Workbook wkb = null;
try
{
excel = new Excel.Application();
wkb = ExcelTools.OfficeUtil.OpenBook(excel, file);
Excel.Worksheet sheet = wkb.Sheets["Data"] as Excel.Worksheet;
Excel.Range range = null;
if (sheet != null)
range = sheet.get_Range("A1", Missing.Value);
string A1 = String.Empty;
if( range != null )
A1 = range.Text.ToString();
Console.WriteLine("A1 value: {0}", A1);
}
catch(Exception ex)
{
//if you need to handle stuff
Console.WriteLine(ex.Message);
}
finally
{
if (wkb != null)
ExcelTools.OfficeUtil.ReleaseRCM(wkb);
if (excel != null)
ExcelTools.OfficeUtil.ReleaseRCM(excel);
}
}
}
}
I'll post the functions from ExcelTools tomorrow, I don't have that code with me either.
Edit:
As promised, here are the Functions from ExcelTools you might need.
public static Excel.Workbook OpenBook(Excel.Application excelInstance, string fileName, bool readOnly, bool editable,
bool updateLinks) {
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
public static void ReleaseRCM(object o) {
try {
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
} catch {
} finally {
o = null;
}
}
To be frank, this stuff is much easier if you use VB.NET. It's in C# because I didn't write it. VB.NET does option parameters well, C# does not, hence the Type.Missing. Once you typed Type.Missing twice in a row, you run screaming from the room!
As for you question, you can try to following:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.find(VS.80).aspx
I will post an example when I get back from my meeting... cheers
Edit: Here is an example
range = sheet.Cells.Find("Value to Find",
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlSearchDirection.xlNext,
Type.Missing,
Type.Missing, Type.Missing);
range.Text; //give you the value found
Here is another example inspired by this site:
range = sheet.Cells.Find("Value to find", Type.Missing, Type.Missing,Excel.XlLookAt.xlWhole,Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlNext,false, false, Type.Missing);
It helps to understand the parameters.
P.S. I'm one of those weird people who enjoys learning COM automation. All this code steamed from a tool I wrote for work which required me to process over 1000+ spreadsheets from the lab each Monday.
You can use Microsoft.Office.Interop.Excel assembly to process excel files.
Right click on your project and go to Add reference. Add the
Microsoft.Office.Interop.Excel assembly.
Include using
Microsoft.Office.Interop.Excel; to make use of assembly.
Here is the sample code:
using Microsoft.Office.Interop.Excel;
//create the Application object we can use in the member functions.
Microsoft.Office.Interop.Excel.Application _excelApp = new Microsoft.Office.Interop.Excel.Application();
_excelApp.Visible = true;
string fileName = "C:\\sampleExcelFile.xlsx";
//open the workbook
Workbook workbook = _excelApp.Workbooks.Open(fileName,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
//select the first sheet
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
//find the used range in worksheet
Range excelRange = worksheet.UsedRange;
//get an object array of all of the cells in the worksheet (their values)
object[,] valueArray = (object[,])excelRange.get_Value(
XlRangeValueDataType.xlRangeValueDefault);
//access the cells
for (int row = 1; row <= worksheet.UsedRange.Rows.Count; ++row)
{
for (int col = 1; col <= worksheet.UsedRange.Columns.Count; ++col)
{
//access each cell
Debug.Print(valueArray[row, col].ToString());
}
}
//clean up stuffs
workbook.Close(false, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(workbook);
_excelApp.Quit();
Marshal.FinalReleaseComObject(_excelApp);
Why don't you create OleDbConnection? There are a lot of available resources in the Internet. Here is an example
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename+";Extended Properties=Excel 8.0");
con.Open();
try
{
//Create Dataset and fill with imformation from the Excel Spreadsheet for easier reference
DataSet myDataSet = new DataSet();
OleDbDataAdapter myCommand = new OleDbDataAdapter(" SELECT * FROM ["+listname+"$]" , con);
myCommand.Fill(myDataSet);
con.Close();
richTextBox1.AppendText("\nDataSet Filled");
//Travers through each row in the dataset
foreach (DataRow myDataRow in myDataSet.Tables[0].Rows)
{
//Stores info in Datarow into an array
Object[] cells = myDataRow.ItemArray;
//Traverse through each array and put into object cellContent as type Object
//Using Object as for some reason the Dataset reads some blank value which
//causes a hissy fit when trying to read. By using object I can convert to
//String at a later point.
foreach (object cellContent in cells)
{
//Convert object cellContect into String to read whilst replacing Line Breaks with a defined character
string cellText = cellContent.ToString();
cellText = cellText.Replace("\n", "|");
//Read the string and put into Array of characters chars
richTextBox1.AppendText("\n"+cellText);
}
}
//Thread.Sleep(15000);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
//Thread.Sleep(15000);
}
finally
{
con.Close();
}
try
{
DataTable sheet1 = new DataTable("Excel Sheet");
OleDbConnectionStringBuilder csbuilder = new OleDbConnectionStringBuilder();
csbuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
csbuilder.DataSource = fileLocation;
csbuilder.Add("Extended Properties", "Excel 12.0 Xml;HDR=YES");
string selectSql = #"SELECT * FROM [Sheet1$]";
using (OleDbConnection connection = new OleDbConnection(csbuilder.ConnectionString))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectSql, connection))
{
connection.Open();
adapter.Fill(sheet1);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
This worked for me. Please try it and let me know for queries.
First of all, it's important to know what you mean by "open an Excel file for reading and copy it to clipboard..."
This is very important because there are many ways you could do that depending just on what you intend to do. Let me explain:
If you want to read a set of data and copy that in the clipboard and you know the data format (e.g. column names), I suggest you use an OleDbConnection to open the file, this way you can treat the xls file content as a Database Table, so you can read data with SQL instruction and treat the data as you want.
If you want to do operations on the data with the Excel object model then open it in the way you began.
Some time it's possible to treat an xls file as a kind of csv file, there are tools like File Helpers which permit you to treat and open an xls file in a simple way by mapping a structure on an arbitrary object.
Another important point is in which Excel version the file is.
I have, unfortunately I say, a strong experience working with Office automation in all ways, even if bounded in concepts like Application Automation, Data Management and Plugins, and generally I suggest only as the last resort, to using Excel automation or Office automation to read data; just if there aren't better ways to accomplish that task.
Working with automation could be heavy in performance, in terms of resource cost, could involve in other issues related for example to security and more, and last but not at least, working with COM interop it's not so "free"..
So my suggestion is think and analyze the situation within your needs and then take the better way.
Here's a 2020 answer - if you don't need to support the older .xls format (so pre 2003) you could use either:
LightweightExcelReader to access specfic cells, or cursor through all the data in a spreadsheet.
or
ExcelToEnumerable if you want to map spreadsheet data to a list of objects.
Pros :
Performance - at the time of writing (the the fastest way to read an .xlsx file)[https://github.com/ChrisHodges/ExcelToEnumerable#performance].
Simplicity - less verbose than OLE DB or OpenXml
Cons:
Neither LightweightExcelReader nor ExcelToEnumerable support .xls files.
Disclaimer: I am the author of LightweightExcelReader and ExcelToEnumerable
Use Open XML.
Here is some code to process a spreadsheet with a specific tab or sheet name and dump it to something like CSV. (I chose a pipe instead of comma).
I wish it was easier to get the value from a cell, but I think this is what we are stuck with. You can see that I reference the MSDN documents where I got most of this code. That is what Microsoft recommends.
/// <summary>
/// Got code from: https://msdn.microsoft.com/en-us/library/office/gg575571.aspx
/// </summary>
[Test]
public void WriteOutExcelFile()
{
var fileName = "ExcelFiles\\File_With_Many_Tabs.xlsx";
var sheetName = "Submission Form"; // Existing tab name.
using (var document = SpreadsheetDocument.Open(fileName, isEditable: false))
{
var workbookPart = document.WorkbookPart;
var sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName);
var worksheetPart = (WorksheetPart)(workbookPart.GetPartById(sheet.Id));
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
foreach (var row in sheetData.Elements<Row>())
{
foreach (var cell in row.Elements<Cell>())
{
Console.Write("|" + GetCellValue(cell, workbookPart));
}
Console.Write("\n");
}
}
}
/// <summary>
/// Got code from: https://msdn.microsoft.com/en-us/library/office/hh298534.aspx
/// </summary>
/// <param name="cell"></param>
/// <param name="workbookPart"></param>
/// <returns></returns>
private string GetCellValue(Cell cell, WorkbookPart workbookPart)
{
if (cell == null)
{
return null;
}
var value = cell.CellFormula != null
? cell.CellValue.InnerText
: cell.InnerText.Trim();
// If the cell represents an integer number, you are done.
// For dates, this code returns the serialized value that
// represents the date. The code handles strings and
// Booleans individually. For shared strings, the code
// looks up the corresponding value in the shared string
// table. For Booleans, the code converts the value into
// the words TRUE or FALSE.
if (cell.DataType == null)
{
return value;
}
switch (cell.DataType.Value)
{
case CellValues.SharedString:
// For shared strings, look up the value in the
// shared strings table.
var stringTable =
workbookPart.GetPartsOfType<SharedStringTablePart>()
.FirstOrDefault();
// If the shared string table is missing, something
// is wrong. Return the index that is in
// the cell. Otherwise, look up the correct text in
// the table.
if (stringTable != null)
{
value =
stringTable.SharedStringTable
.ElementAt(int.Parse(value)).InnerText;
}
break;
case CellValues.Boolean:
switch (value)
{
case "0":
value = "FALSE";
break;
default:
value = "TRUE";
break;
}
break;
}
return value;
}
Use OLEDB Connection to communicate with excel files. it gives better result
using System.Data.OleDb;
string physicalPath = "Your Excel file physical path";
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
String strNewPath = physicalPath;
String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
String query = "SELECT * FROM [Sheet1$]"; // You can use any different queries to get the data from the excel sheet
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed) conn.Open();
try
{
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
catch
{
// Exception Msg
}
finally
{
da.Dispose();
conn.Close();
}
The Output data will be stored in dataset, using the dataset object you can easily access the datas.
Hope this may helpful
Using OlebDB, we can read excel file in C#, easily, here is the code while working with Web-Form, where FileUpload1 is file uploading tool
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//get file path
filePath = path + Path.GetFileName(FileUpload1.FileName);
//get file extenstion
string extension = Path.GetExtension(FileUpload1.FileName);
//save file on "Uploads" folder of project
FileUpload1.SaveAs(filePath);
string conString = string.Empty;
//check file extension
switch (extension)
{
case ".xls": //Excel 97-03.
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Excel03ConString;Extended Properties='Excel 8.0;HDR=YES'";
break;
case ".xlsx": //Excel 07 and above.
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Excel07ConString;Extended Properties='Excel 8.0;HDR=YES'";
break;
}
//create datatable object
DataTable dt = new DataTable();
conString = string.Format(conString, filePath);
//Use OldDb to read excel
using (OleDbConnection connExcel = new OleDbConnection(conString))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//Get the name of First Sheet.
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet.
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
}
}
}
//bind datatable with GridView
GridView1.DataSource = dt;
GridView1.DataBind();
Source : https://qawithexperts.com/article/asp-net/read-excel-file-and-import-data-into-gridview-using-datatabl/209
Console application similar code example
https://qawithexperts.com/article/c-sharp/read-excel-file-in-c-console-application-example-using-oledb/168
If you need don't want to use OleDB, you can try https://github.com/ExcelDataReader/ExcelDataReader
which seems to have the ability to handle both formats (.xls and .xslx)
Excel File Reader & Writer Without Excel On u'r System
Download and add the dll for
NPOI u'r project.
Using this code to read a excel file.
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XSSFWorkbook XSSFWorkbook = new XSSFWorkbook(file);
}
ISheet objxlWorkSheet = XSSFWorkbook.GetSheetAt(0);
int intRowCount = 1;
int intColumnCount = 0;
for (; ; )
{
IRow Row = objxlWorkSheet.GetRow(intRowCount);
if (Row != null)
{
ICell Cell = Row.GetCell(0);
ICell objCell = objxlWorkSheet.GetRow(intRowCount).GetCell(intColumnCount); }}
You can use ExcelDataReader see GitHub
You need to install nugets :
-ExcelDataReader
-ExcelDataReader.DataSet
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.IO;
using ExcelDataReader;
using System.Text;
/// <summary>
/// Excel parsing in this class is performed by using a common shareware Lib found on:
/// https://github.com/ExcelDataReader/ExcelDataReader
/// </summary>
public static class ExcelParser
{
/// <summary>
/// Load, read and get values from Excel sheet
/// </summary>
public static List<FileRow> GetExcelRows(string path, string sheetName, bool skipFirstLine)
{
if (File.Exists(path))
{
return GetValues(path, sheetName, skipFirstLine);
}
else
throw new Exception("The process cannot access the file");
}
/// <summary>
/// Parse sheet names from given Excel file.
/// </summary>
public static List<string> GetSheetNames(string path)
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (var excelReader = GetExcelDataReader(path, stream))
{
var dataset = excelReader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
var names = from DataTable table in dataset.Tables
select table.TableName;
return names.ToList();
}
}
}
/// <summary>
/// Parse values from Excel sheet and add to Rows collection.
/// </summary>
public static List<FileRow> GetValues(string path, string sheetName, bool skipFirstLine)
{
var rowItems = new List<FileRow>();
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var excelReader = GetExcelDataReader(path, stream))
{
var dataset = excelReader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
{
UseHeaderRow = skipFirstLine
}
});
foreach (DataRow row in dataset.Tables[sheetName].Rows)
{
var rowItem = new FileRow();
foreach (var value in row.ItemArray)
rowItem.Values.Add(value);
rowItems.Add(rowItem);
}
}
}
return rowItems;
}
private static IExcelDataReader GetExcelDataReader(string path, Stream stream)
{
var extension = GetExtension(path);
switch (extension)
{
case "xls":
return ExcelReaderFactory.CreateBinaryReader(stream);
case "xlsx":
return ExcelReaderFactory.CreateOpenXmlReader(stream);
default:
throw new Exception(string.Format("'{0}' is not a valid Excel extension", extension));
}
}
private static string GetExtension(string path)
{
var extension = Path.GetExtension(path);
return extension == null ? null : extension.ToLower().Substring(1);
}
}
With this entity :
public class FileRow
{
public List<object> Values { get; set; }
public FileRow()
{
Values = new List<object>();
}
}
Use like that :
var txtPath = #"D:\Path\excelfile.xlsx";
var sheetNames = ExcelParser.GetSheetNames(txtPath);
var datas = ExcelParser.GetExcelRows(txtPath, sheetNames[0], true);
The recommended way to read Excel files on server side app is Open XML.
Sharing few links -
https://msdn.microsoft.com/en-us/library/office/hh298534.aspx
https://msdn.microsoft.com/en-us/library/office/ff478410.aspx
https://msdn.microsoft.com/en-us/library/office/cc823095.aspx
public void excelRead(string sheetName)
{
Excel.Application appExl = new Excel.Application();
Excel.Workbook workbook = null;
try
{
string methodName = "";
Excel.Worksheet NwSheet;
Excel.Range ShtRange;
//Opening Excel file(myData.xlsx)
appExl = new Excel.Application();
workbook = appExl.Workbooks.Open(sheetName, Missing.Value, ReadOnly: false);
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
ShtRange = NwSheet.UsedRange; //gives the used cells in sheet
int rCnt1 = 0;
int cCnt1 = 0;
for (rCnt1 = 1; rCnt1 <= ShtRange.Rows.Count; rCnt1++)
{
for (cCnt1 = 1; cCnt1 <= ShtRange.Columns.Count; cCnt1++)
{
if (Convert.ToString(NwSheet.Cells[rCnt1, cCnt1].Value2) == "Y")
{
methodName = NwSheet.Cells[rCnt1, cCnt1 - 2].Value2;
Type metdType = this.GetType();
MethodInfo mthInfo = metdType.GetMethod(methodName);
if (Convert.ToString(NwSheet.Cells[rCnt1, cCnt1 - 2].Value2) == "fn_AddNum" || Convert.ToString(NwSheet.Cells[rCnt1, cCnt1 - 2].Value2) == "fn_SubNum")
{
StaticVariable.intParam1 = Convert.ToInt32(NwSheet.Cells[rCnt1, cCnt1 + 3].Value2);
StaticVariable.intParam2 = Convert.ToInt32(NwSheet.Cells[rCnt1, cCnt1 + 4].Value2);
object[] mParam1 = new object[] { StaticVariable.intParam1, StaticVariable.intParam2 };
object result = mthInfo.Invoke(this, mParam1);
StaticVariable.intOutParam1 = Convert.ToInt32(result);
NwSheet.Cells[rCnt1, cCnt1 + 5].Value2 = Convert.ToString(StaticVariable.intOutParam1) != "" ? Convert.ToString(StaticVariable.intOutParam1) : String.Empty;
}
else
{
object[] mParam = new object[] { };
mthInfo.Invoke(this, mParam);
NwSheet.Cells[rCnt1, cCnt1 + 5].Value2 = StaticVariable.outParam1 != "" ? StaticVariable.outParam1 : String.Empty;
NwSheet.Cells[rCnt1, cCnt1 + 6].Value2 = StaticVariable.outParam2 != "" ? StaticVariable.outParam2 : String.Empty;
}
NwSheet.Cells[rCnt1, cCnt1 + 1].Value2 = StaticVariable.resultOut;
NwSheet.Cells[rCnt1, cCnt1 + 2].Value2 = StaticVariable.resultDescription;
}
else if (Convert.ToString(NwSheet.Cells[rCnt1, cCnt1].Value2) == "N")
{
MessageBox.Show("Result is No");
}
else if (Convert.ToString(NwSheet.Cells[rCnt1, cCnt1].Value2) == "EOF")
{
MessageBox.Show("End of File");
}
}
}
workbook.Save();
workbook.Close(true, Missing.Value, Missing.Value);
appExl.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ShtRange);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(NwSheet);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(appExl);
}
catch (Exception)
{
workbook.Close(true, Missing.Value, Missing.Value);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.CleanupUnusedObjectsInCurrentContext();
}
}
//code for reading excel data in datatable
public void testExcel(string sheetName)
{
try
{
MessageBox.Show(sheetName);
foreach(Process p in Process.GetProcessesByName("EXCEL"))
{
p.Kill();
}
//string fileName = "E:\\inputSheet";
Excel.Application oXL;
Workbook oWB;
Worksheet oSheet;
Range oRng;
// creat a Application object
oXL = new Excel.Application();
// get WorkBook object
oWB = oXL.Workbooks.Open(sheetName);
// get WorkSheet object
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];
System.Data.DataTable dt = new System.Data.DataTable();
//DataSet ds = new DataSet();
//ds.Tables.Add(dt);
DataRow dr;
StringBuilder sb = new StringBuilder();
int jValue = oSheet.UsedRange.Cells.Columns.Count;
int iValue = oSheet.UsedRange.Cells.Rows.Count;
// get data columns
for (int j = 1; j <= jValue; j++)
{
oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, j];
string strValue = oRng.Text.ToString();
dt.Columns.Add(strValue, System.Type.GetType("System.String"));
}
//string colString = sb.ToString().Trim();
//string[] colArray = colString.Split(':');
// get data in cell
for (int i = 2; i <= iValue; i++)
{
dr = dt.NewRow();
for (int j = 1; j <= jValue; j++)
{
oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
string strValue = oRng.Text.ToString();
dr[j - 1] = strValue;
}
dt.Rows.Add(dr);
}
if(StaticVariable.dtExcel != null)
{
StaticVariable.dtExcel.Clear();
StaticVariable.dtExcel = dt.Copy();
}
else
StaticVariable.dtExcel = dt.Copy();
oWB.Close(true, Missing.Value, Missing.Value);
oXL.Quit();
MessageBox.Show(sheetName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
//code for class initialize
public static void startTesting(TestContext context)
{
Playback.Initialize();
ReadExcel myClassObj = new ReadExcel();
string sheetName="";
StreamReader sr = new StreamReader(#"E:\SaveSheetName.txt");
sheetName = sr.ReadLine();
sr.Close();
myClassObj.excelRead(sheetName);
myClassObj.testExcel(sheetName);
}
//code for test initalize
public void runValidatonTest()
{
DataTable dtFinal = StaticVariable.dtExcel.Copy();
for (int i = 0; i < dtFinal.Rows.Count; i++)
{
if (TestContext.TestName == dtFinal.Rows[i][2].ToString() && dtFinal.Rows[i][3].ToString() == "Y" && dtFinal.Rows[i][4].ToString() == "TRUE")
{
MessageBox.Show(TestContext.TestName);
MessageBox.Show(dtFinal.Rows[i][2].ToString());
StaticVariable.runValidateResult = "true";
break;
}
}
//StaticVariable.dtExcel = dtFinal.Copy();
}
I'd recommend you to use Bytescout Spreadsheet.
https://bytescout.com/products/developer/spreadsheetsdk/bytescoutspreadsheetsdk.html
I tried it with Monodevelop in Unity3D and it is pretty straight forward. Check this sample code to see how the library works:
https://bytescout.com/products/developer/spreadsheetsdk/read-write-excel.html

Categories

Resources