System.NullReferenceException: Excel C# - c#

I have written a class that deals with opening, writing to, and closing an Excel file.
The methods in the Excel class are being called by a different call. When I call the addDatatoExcel method the first time, everything is working, however when I call it again, I get a "System.NullReferenceException: Excel C#". VB says I am passing a null object. I know what the error is I just can not figure out how to prevent it.
public class ExcelFile
{
public static ExcelFile C1;
private string excelFilePath = "‪‪C:\\Final.xlsx";
private int rowNumber = 1; // define first row number to enter data in Excel
Excel.Application excelApp;
Excel.Workbook excelWorkbook;
Excel.Worksheet excelWorksheet;
int ExcelCounter = 0;
int checker = 0;
string str4 = "h";
public void openExcel()
{
excelApp = null;
excelApp = new Excel.Application(); // create Excel App
excelWorkbook = excelApp.Workbooks.Add();
excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets.Add();
}
public void addDataToExcel(string str4)
{
excelWorksheet.Cells[5, 1] = str4;
ExcelCounter++;
}
public void closeExcel()
{
excelWorkbook.Close();
excelApp.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorksheet);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorkbook);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
I want to be able to add data to the first same Excel sheet that was created.
This is the method that calls the functions in the Excel class.
public void AddTextToLabel(string str)
{
ExcelFile _Excel = new ExcelFile();
if (flag == 1) // Celsius
{
//list Adding Function 1;
temp = Double.Parse(str);
x++;
list.Add(x, temp);
zedGraphControl1.Invalidate();
CreateChart(zedGraphControl1);
//Example();
if (Excelcounter == 0)
{
_Excel.openExcel();
Excelcounter++;
}
_Excel.addDataToExcel(str);
if (Excelcounter == 15)
{
_Excel.closeExcel();
}
}
}
I have checked and the variable str that is being passed to addDatatoExcel is not null. There are other parameters that change to null when the function is called the second time. Is this because I am not saving the Excel file between every call to AddDatatoExecl();
1st call to function
2nd call to function
I am new to C# so please be specific. Thanks

You are opening the Excel, only the first time:
// Each time, you call this:
ExcelFile _Excel = new ExcelFile();
...
//The first time, this is called
if (Excelcounter == 0)
{
_Excel.openExcel();
Excelcounter++;
}
Then the next time you call the AddTextToLabel function
// A new instance is created
ExcelFile _Excel = new ExcelFile();
// The excel counter is not 0 so the _Excel.openExcel() will not be called.
if (Excelcounter == 0)
Change your code to this:
// Now your excel file is an instance member
// as is your Excelcounter
ExcelFile _Excel = new ExcelFile();
public void AddTextToLabel(string str)
{
if (flag == 1) // Celcuis
{
//list Adding Function 1;
temp = Double.Parse(str);
x++;
list.Add(x, temp);
zedGraphControl1.Invalidate();
CreateChart(zedGraphControl1);
//Example();
if (Excelcounter == 0)
{
_Excel.openExcel();
Excelcounter++;
}
_Excel.addDataToExcel(str);
if (Excelcounter == 15)
{
_Excel.closeExcel();
}
}

Related

How to release an Excel Object

In my code below I have a commented out foreach loop that works except it does not release the excel object due to the background enum. So I am trying to convert it to a for loop but up until this point I have avoided 'for' loops and I can't seem to get mine to work. I am just pointing out that I know it is broke.
I did comment out the broken 'for' loop and it runs and all works as expected except I can't release the excel objects...
My question, I have tried so many different examples but I can not release the excel object from my task manager and it remains open in the back ground.
I use the "One Dot" rule
I'm using a try/final
I have tried many different Marshal.ReleaseComOjbect(obj), examples from the web
I am using a 'for loop' (not a foreach loop) so no hidden Enum object
Thank you for any assistance, education
My code:
public static List<ExcelObject> listOfExcelObjects = new List<ExcelObject>();
public static void txtSearchBar_Changed(object sender, EventArgs e)
{
Excel.Application excel = null;
Excel.Workbooks workbooks = null;
Excel.Workbook workbook = null;
Excel.Sheets sheets = null;
Excel.Worksheet worksheet = null;
Excel.Range range = null;
try
{
excel = new Excel.Application();
workbooks = excel.Workbooks;
workbook = workbooks.Open(GlobalObject.filePath, Notify: false, ReadOnly: true);
sheets = workbook.Sheets;
for (int i = 1; i < sheets.Count; i++)
{
worksheet = sheets.Item[i];
if (worksheet.Visible == Excel.XlSheetVisibility.xlSheetVisible)
{
Form_MainForm.thisForm.cmbx_WorkSheet.Items.Add(sheets.Item[i].Name);
if (worksheet.Name == "UNIT FEEDER")
{
Form_MainForm.thisForm.cmbx_WorkSheet.Text = worksheet.Name;
worksheet.Select();
Form_MainForm.thisForm.txtBox_StartingRow.Text = $"11";
Form_MainForm.thisForm.txtBox_EndingRow.Text = $"{_Events.EndOfRow()}";
range = worksheet.Range[$"A{Int32.Parse(Form_MainForm.thisForm.txtBox_StartingRow.Text)}", $"A{Int32.Parse(_Events.EndOfRow())}"];
for (int j = 1; j < range.Cells.Count; j++)
{
_Events.listOfExcelObjects.Add(new ExcelObject() { FeederNumber = range.Cells.Item[j] });
}
//foreach (Excel.Range j in worksheet.Range[$"A{Form_MainForm.thisForm.txtBox_StartingRow.Text}", $"A{_Events.EndOfRow()}"].Cells)
//{
// _Events.listOfExcelObjects.Add(new ExcelObject() { FeederNumber = j.Value });
//}
Form_MainForm.thisForm.grd_DataGridView.DataSource = _Events.listOfExcelObjects;
}
}
}
}
finally
{
releaseObject(range);
releaseObject(worksheet);
releaseObject(sheets);
releaseObject(workbook);
releaseObject(workbooks);
releaseObject(excel);
//Marshal.ReleaseComObject(range);
//Marshal.ReleaseComObject(worksheet);
//Marshal.ReleaseComObject(sheets);
//Marshal.ReleaseComObject(workbook);
//Marshal.ReleaseComObject(workbooks);
//Marshal.ReleaseComObject(excel);
}
}
private static void releaseObject(object obj)
{
if (obj != null && Marshal.IsComObject(obj))
{
Marshal.ReleaseComObject(obj);
}
obj = null;
}
You need to close Excel:
excel.Quit();
For those who don't read the comment section 'such as myself'
I have a function EndOfRow() that I did not do any cleaning to release objects. I didn't know about releasing objects when I created this function and I forgot about it. Many hours spent on a simple oversight.
Leaving out the Quit() was also an oversight in my many attemts to resolve my issue but was not the main problem, EndOfRow() was...
So what I learned is to release the objects as soon as I am done using excel.
Thanks Terry

Writing to sheet leads to "name already taken"

I'm learning how to use Interop.Excel. The test Winforms program reads an existing Excel file, checks if a tab names "Added_by_program" exists, deletes the sheet if it does, and creates a new sheet named "Added_by_program." If I don't try to write to the new sheet, the program runs perfectly, over and over. I get problems when I try to write to it. If the sheet is not present in the original file, the program runs perfectly one time, and writes correctly to the newly created sheet. but on subsequent runs, I get:
"System.Runtime.InteropServices.COMException: 'That name is already taken. Try a different one.'"
for the line that tries to name the new sheet. I have to manually kill the open Excel instance. What am I missing?
Code (irrelevant lines taken out)
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace excelReadWrite
{
public partial class Form1 : Form
{
string readFolder = myPath;
string inFileName = #"Aram test excel file.xlsx";
string newSheetName = "Added_by_program";
Range rawRange = null;
Range pasteRange = null;
int rawCols = 0;
int rawRows = 0;
int iInSheet = 0;
int iNewSheet = 0;
int nInSheets = 0;
bool foundRawSheet = false;
bool foundNewSheet = false;
Worksheet worksheet = null;
public Form1()
{
InitializeComponent();
}
private void start_Button_Click(object sender, EventArgs e)
{
string inFile = myPath+ inFileName;
int nSheets = 0;
string sheetNames = "";
// Open Excel workbook to read
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook = xl.Workbooks.Open(inFile);
// Count worksheets in opened Excel file
nSheets = workbook.Worksheets.Count;
nSheets_TextBox.Text = nSheets.ToString();
nInSheets = 0;
foreach (Worksheet worksheet in workbook.Worksheets)
++nInSheets;
//foreach (Worksheet worksheet in workbook.Worksheets)
for (int iSheet = nInSheets; iSheet >= 1; --iSheet)
{
worksheet = workbook.Worksheets[iSheet];
sheetNames += " " + worksheet.Name;
// The program is going to add a worksheet. If it already exists, delete it before adding it.
if (string.Equals(worksheet.Name, newSheetName))
{
workbook.Worksheets[iSheet].Delete();
}
}
// Add a new sheet and name it
if (foundRawSheet)
{
newWorksheet = workbook.Worksheets.Add();
newWorksheet.Name = newSheetName;
// THE NEXT LINE IS THE PROBLEM LINE
// "Written" WILL BE WRITTEN TO A1:C3 WHEN THE SHEET IS CREATED, BUT THIS LINE
// CAUSES THE ERROR IN SUBSEQUENT RUNS
// IF I COMMENT IT OUT, THE PROGRAM RUNS FINE, REPEATEDLY
newWorksheet.Range["A1", "C3"].Value2 = "Written";
workbook.Save();
workbook.Close();
xl.Quit();
}
}
}
Did you set xl.DisplayAlerts=false?
If not, deleting a worksheet with existing data will cause a confirm dialog to be displayed. .
If the Excel application is visible, the Worksheet.Delete will block until the dialog is acknowledged.
If the Excel application is not visible, your code execution will proceed (the dialog is effectively canceled --> delete not confirmed), but the worksheet will not be deleted.

Workbook is Created with two worksheets in C# and Always ask for overwriting with Book7, Book8, etc

I want to create an Excel Workbook with one WorkSheet, then Save the data and close it. But it creates two Sheets. Sheet2 and Sheet1. It writes the data successfully though. Everytime I delete the xlsx file manually, it asks me for overwriting Book8.xlsx even though there is not such a file with this name.
I just want one worksheet and I want to have it with a specific name too, instead of Sheet1
class Program
{
static void Main(string[] args)
{
Excel excel = new Excel(#"d:\Test.xlsx", 1);
excel.WriteToCell(0, 0, "Test2");
excel.Save();
excel.SaveAs(#"d:\Test.xlsx");
excel.Close();
}
}
Inside Excel.cs
using Microsoft.Office.Interop.Excel;
using _Excel = Microsoft.Office.Interop.Excel;
namespace Excel
{
class Excel
{
_Application excel = new _Excel.Application();
Workbook wb;
Worksheet ws;
public Excel(string path, int Sheet)
{
excel.Visible = false;
wb = excel.Workbooks.Add();
ws = wb.Worksheets.Add();
}
public void WriteToCell(int i, int j, string s)
{
i++;
j++;
ws.Cells[i, j].Value = s;
}
public void Save()
{
wb.Save();
}
public void SaveAs(string path)
{
wb.SaveAs(path);
}
public void Close()
{
wb.Close();
}
}
}
Remove the line ws = wb.Worksheets.Add(); from Excel constructor to not create the second sheet.
public Excel(string path, int Sheet)
{
excel.Visible = false;
wb = excel.Workbooks.Add();
ws = (Worksheet)wb.Worksheets[1];
if (ws == null)
{
Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
}
}
To rename the worksheet:
public RenameWorksheet(string newName)
{
ws.Name = newName;
}
If you delete a file from a directory, it should be deleted. However, if you are having a pop saying the file exists and do you want to replace it then my guess is it is probably a human error here. Maybe you are looking under a wrong directory? Happens to the best of us at times ;) Either way as a programmer you can choose to delete the file if it exists before creation to avoid human errors:
if(File.Exists(fileName))
{
File.Delete(fileName);
}
or simply rename it if you don't want to replace it.
EDIT
On second look I realized you are creating a new file:
Excel excel = new Excel(#"d:\Test.xlsx", 1);
and then calling:
excel.SaveAs(#"d:\Test.xlsx");
what else do you expect?

C# Interop the name does not exist in the current context

I'm using C# Interop to get the Values from an Excel Worksheet depending on a parameter passed to a function and I get the following error:
the name 'sheet' does not exist in the current context
This is my code:
public void getIndexes(int num)
{
var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
var wsEvars = wb.Sheets["Evars"];
var wsEvents = wb.Sheets["Events"];
if (num == 0)
{
var sheet = wsEvars;
}
if (num == 2)
{
var sheet = wsEvents;
}
if (num != 2)
{
var rng = (Excel.Range)sheet.Range[sheet.Cells[3, 2], sheet.Cells[3, 25]];
}
}
I suppose that the sheet variable should be initialized before the first if statement...but which should be the type of this variable since it's a COM Object?
The type you're looking for is Excel.Worksheet.
As you correctly surmised you can just declare it before the first if statement.
public void getIndexes(int num)
{
var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
var wsEvars = wb.Sheets["Evars"];
var wsEvents = wb.Sheets["Events"];
Excel.Worksheet sheet = null; // Declared here
if (num == 0)
{
sheet = wsEvars;
// Rest of code
...
For reference you can hover over the keyword var and the tooltip tells you the type it will compile to.
If you've got a reference set to the Excel interop anyway then declare them like so:
Excel.Worksheet sheet = null;
Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.Worksheet wsEvars = wb.Sheets["Evars"];
Excel.Worksheet wsEvents = wb.Sheets["Events"];

c# (WinForms-App) export DataSet to Excel

I need a solution to export a dataset to an excel file without any asp code (HttpResonpsne...) but i did not find a good example to do this...
Best thanks in advance
I've created a class that exports a DataGridView or DataTable to an Excel file. You can probably change it a bit to make it use your DataSet instead (iterating through the DataTables in it). It also does some basic formatting which you could also extend.
To use it, simply call ExcelExport, and specify a filename and whether to open the file automatically or not after exporting. I also could have made them extension methods, but I didn't. Feel free to.
Note that Excel files can be saved as a glorified XML document and this makes use of that.
EDIT: This used to use a vanilla StreamWriter, but as pointed out, things would not be escaped correctly in many cases. Now it uses a XmlWriter, which will do the escaping for you.
The ExcelWriter class wraps an XmlWriter. I haven't bothered, but you might want to do a bit more error checking to make sure you can't write cell data before starting a row, and such. The code is below.
public class ExcelWriter : IDisposable
{
private XmlWriter _writer;
public enum CellStyle { General, Number, Currency, DateTime, ShortDate };
public void WriteStartDocument()
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"");
_writer.WriteStartElement("ss", "Workbook", "urn:schemas-microsoft-com:office:spreadsheet");
WriteExcelStyles();
}
public void WriteEndDocument()
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteEndElement();
}
private void WriteExcelStyleElement(CellStyle style)
{
_writer.WriteStartElement("Style", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("ID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
_writer.WriteEndElement();
}
private void WriteExcelStyleElement(CellStyle style, string NumberFormat)
{
_writer.WriteStartElement("Style", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("ID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
_writer.WriteStartElement("NumberFormat", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("Format", "urn:schemas-microsoft-com:office:spreadsheet", NumberFormat);
_writer.WriteEndElement();
_writer.WriteEndElement();
}
private void WriteExcelStyles()
{
_writer.WriteStartElement("Styles", "urn:schemas-microsoft-com:office:spreadsheet");
WriteExcelStyleElement(CellStyle.General);
WriteExcelStyleElement(CellStyle.Number, "General Number");
WriteExcelStyleElement(CellStyle.DateTime, "General Date");
WriteExcelStyleElement(CellStyle.Currency, "Currency");
WriteExcelStyleElement(CellStyle.ShortDate, "Short Date");
_writer.WriteEndElement();
}
public void WriteStartWorksheet(string name)
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteStartElement("Worksheet", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("Name", "urn:schemas-microsoft-com:office:spreadsheet", name);
_writer.WriteStartElement("Table", "urn:schemas-microsoft-com:office:spreadsheet");
}
public void WriteEndWorksheet()
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteEndElement();
_writer.WriteEndElement();
}
public ExcelWriter(string outputFileName)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
_writer = XmlWriter.Create(outputFileName, settings);
}
public void Close()
{
if (_writer == null) throw new InvalidOperationException("Already closed.");
_writer.Close();
_writer = null;
}
public void WriteExcelColumnDefinition(int columnWidth)
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteStartElement("Column", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteStartAttribute("Width", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteValue(columnWidth);
_writer.WriteEndAttribute();
_writer.WriteEndElement();
}
public void WriteExcelUnstyledCell(string value)
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteStartElement("Cell", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteStartElement("Data", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "String");
_writer.WriteValue(value);
_writer.WriteEndElement();
_writer.WriteEndElement();
}
public void WriteStartRow()
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteStartElement("Row", "urn:schemas-microsoft-com:office:spreadsheet");
}
public void WriteEndRow()
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteEndElement();
}
public void WriteExcelStyledCell(object value, CellStyle style)
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
_writer.WriteStartElement("Cell", "urn:schemas-microsoft-com:office:spreadsheet");
_writer.WriteAttributeString("StyleID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
_writer.WriteStartElement("Data", "urn:schemas-microsoft-com:office:spreadsheet");
switch (style)
{
case CellStyle.General:
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "String");
break;
case CellStyle.Number:
case CellStyle.Currency:
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "Number");
break;
case CellStyle.ShortDate:
case CellStyle.DateTime:
_writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "DateTime");
break;
}
_writer.WriteValue(value);
// tag += String.Format("{1}\"><ss:Data ss:Type=\"DateTime\">{0:yyyy\\-MM\\-dd\\THH\\:mm\\:ss\\.fff}</ss:Data>", value,
_writer.WriteEndElement();
_writer.WriteEndElement();
}
public void WriteExcelAutoStyledCell(object value)
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
//write the <ss:Cell> and <ss:Data> tags for something
if (value is Int16 || value is Int32 || value is Int64 || value is SByte ||
value is UInt16 || value is UInt32 || value is UInt64 || value is Byte)
{
WriteExcelStyledCell(value, CellStyle.Number);
}
else if (value is Single || value is Double || value is Decimal) //we'll assume it's a currency
{
WriteExcelStyledCell(value, CellStyle.Currency);
}
else if (value is DateTime)
{
//check if there's no time information and use the appropriate style
WriteExcelStyledCell(value, ((DateTime)value).TimeOfDay.CompareTo(new TimeSpan(0, 0, 0, 0, 0)) == 0 ? CellStyle.ShortDate : CellStyle.DateTime);
}
else
{
WriteExcelStyledCell(value, CellStyle.General);
}
}
#region IDisposable Members
public void Dispose()
{
if (_writer == null)
return;
_writer.Close();
_writer = null;
}
#endregion
}
Then you can export your DataTable using the following:
public static void ExcelExport(DataTable data, String fileName, bool openAfter)
{
//export a DataTable to Excel
DialogResult retry = DialogResult.Retry;
while (retry == DialogResult.Retry)
{
try
{
using (ExcelWriter writer = new ExcelWriter(fileName))
{
writer.WriteStartDocument();
// Write the worksheet contents
writer.WriteStartWorksheet("Sheet1");
//Write header row
writer.WriteStartRow();
foreach (DataColumn col in data.Columns)
writer.WriteExcelUnstyledCell(col.Caption);
writer.WriteEndRow();
//write data
foreach (DataRow row in data.Rows)
{
writer.WriteStartRow();
foreach (object o in row.ItemArray)
{
writer.WriteExcelAutoStyledCell(o);
}
writer.WriteEndRow();
}
// Close up the document
writer.WriteEndWorksheet();
writer.WriteEndDocument();
writer.Close();
if (openAfter)
OpenFile(fileName);
retry = DialogResult.Cancel;
}
}
catch (Exception myException)
{
retry = MessageBox.Show(myException.Message, "Excel Export", MessageBoxButtons.RetryCancel, MessageBoxIcon.Asterisk);
}
}
}
The following site demonstrates how to export a DataSet (or DataTable or List<>) into a "genuine" Excel 2007 .xlsx file.
It uses the OpenXML libraries, so you don't need to have Excel installed on your server.
C# ExportToExcel library
All of the source code is provided, free of charge, along with instructions using it with ASP.NET, ASP.NET Core 2+ or regular C#.
It's very easy to add to your own applications, you just need to call one function, passing in an Excel filename, and your data source:
DataSet ds = CreateSampleData();
string excelFilename = "C:\\Sample.xlsx";
CreateExcelFile.CreateExcelDocument(ds, excelFilename);
Hope this helps.
Creating excel files in .NET applications is quite common and similar questions have been asked several times before. For instance here and here. The last question asks about reading excel files, but most suggested solutions should work both ways.
This was a post with a really helpful top answer, but I found it was lacking because there was no simple way to import the XML file back into a datatable. I ended up having to write my own, and thought I would share it here in case anyone else was in the same boat (google was exceptionally unhelpful in that regard):
public static DataTable ImportExcelXML(string Filename)
{
//create a new dataset to load in the XML file
DataSet DS = new DataSet();
//Read the XML file into the dataset
DS.ReadXml(Filename);
//Create a new datatable to store the raw Data
DataTable Raw = new DataTable();
//assign the raw data from the file to the datatable
Raw = DS.Tables["Data"];
//count the number of columns in the XML file
int ColumnNumber = Raw.Columns.Count;
//create a datatable to store formatted Import Data
DataTable ImportData = new DataTable();
//create a string list to store the cell data of each row
List<string> RowData = new List<string>();
//loop through each row in the raw data table
for (int Counter = 0; Counter < Raw.Rows.Count; Counter++)
{
//if the data in the row is a colum header
if (Counter < ColumnNumber)
{
//add the column name to our formatted datatable
ImportData.Columns.Add(Raw.Rows[Counter].ItemArray.GetValue(1).ToString());
}
else
{
//if the row # (1 row = 1 cell from the excel file) from the raw datatable is divisable evenly by the number of columns in the formated import datatable AND this is not the 1st row of the raw table data after the headers
if ((Counter % ColumnNumber == 0) && (Counter != ColumnNumber))
{
//add the row we just built to the formatted import datatable
ImportData.Rows.Add(GenerateRow(ImportData, RowData));
//clear rowdata list in preperation for the next row
RowData.Clear();
}
//add the current cell data value from the raw datatable to the string list of cell values for the next row to be added to the formatted input datatable
RowData.Add(Raw.Rows[Counter].ItemArray.GetValue(1).ToString());
}
}
//add the final row
ImportData.Rows.Add(GenerateRow(ImportData, RowData));
return ImportData;
}
public static DataRow GenerateRow(DataTable ImportData, List<string> RowData)
{
//create a counter to keep track of the column position during row composition
int ColumnPosition = 0;
//make a new datarow based on the schema of the formated import datatable
DataRow NewRow = ImportData.NewRow();
//for each string cell value collected for the RowData list for this row
foreach (string CellData in RowData)
{
//add the cell value to the new datarow
NewRow[ImportData.Columns[ColumnPosition].ColumnName] = CellData;
//incriment column position in the new row
ColumnPosition++;
}
//return the generated row
return NewRow;
}
the code have problem with null values.
public void WriteExcelAutoStyledCell(object value)
{
//solve null values
if (value is DBNull) return;
I would have added this to the comments but I am new to stack so I am unable to comment. Using lc.'s solution I added another function that tests the strings characters for invalid XML characters. When I was exporting to excel on occasion there were characters that were found that were causing the export to fail.
You will need to modify one of the functions that is in lc's code.
public void WriteExcelAutoStyledCell(object value)
{
if (_writer == null) throw new InvalidOperationException("Cannot write after closing.");
string newValue = string.Empty;
try
{
//write the <ss:Cell> and <ss:Data> tags for something
if (value is Int16 || value is Int32 || value is Int64 || value is SByte ||
value is UInt16 || value is UInt32 || value is UInt64 || value is Byte)
{
WriteExcelStyledCell(value, CellStyle.Number);
}
else if (value is Single || value is Double || value is Decimal) //we'll assume it's a currency
{
WriteExcelStyledCell(value, CellStyle.Currency);
}
else if (value is DateTime)
{
//check if there's no time information and use the appropriate style
WriteExcelStyledCell(value, ((DateTime)value).TimeOfDay.CompareTo(new TimeSpan(0, 0, 0, 0, 0)) == 0 ? CellStyle.ShortDate : CellStyle.DateTime);
}
else
{
newValue = CheckXmlCompatibleValues(value.ToString()).ToString();
WriteExcelStyledCell(newValue, CellStyle.General);
}
}
catch (Exception thisException)
{
throw new InvalidOperationException(thisException.Message.ToString());
}
}
And add this function to 'ExcelWriter' class
public string CheckXmlCompatibleValues(string value)
{
string newValue = string.Empty;
bool found = false;
foreach (char c in value)
{
if (XmlConvert.IsXmlChar(c))
newValue += c.ToString();
else
found = true;
}
return newValue.ToString();
}
lc. Thanks for the code!
using XL = Microsoft.Office.Interop.Excel;
using System.Reflection;
public static void Datasource(DataTable dt)
{
XL.Application oXL;
XL._Workbook oWB;
XL._Worksheet oSheet;
XL.Range oRng;
try
{
oXL = new XL.Application();
Application.DoEvents();
oXL.Visible = false;
//Get a new workbook.
oWB = (XL._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (XL._Worksheet)oWB.ActiveSheet;
//System.Data.DataTable dtGridData=ds.Tables[0];
int iRow = 2;
if (dt.Rows.Count > 0)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
oSheet.Cells[1, j + 1] = dt.Columns[j].ColumnName;
}
// For each row, print the values of each column.
for (int rowNo = 0; rowNo < dt.Rows.Count; rowNo++)
{
for (int colNo = 0; colNo < dt.Columns.Count; colNo++)
{
oSheet.Cells[iRow, colNo + 1] = dt.Rows[rowNo][colNo].ToString();
}
iRow++;
}
iRow++;
}
oRng = oSheet.get_Range("A1", "IV1");
oRng.EntireColumn.AutoFit();
oXL.Visible = true;
}
catch (Exception theException)
{
throw theException;
}
finally
{
oXL = null;
oWB = null;
oSheet = null;
oRng = null;
}
}
Import from Excel to datatable:
DataTable dtTable = new DataTable();
DataColumn col = new DataColumn("Rfid");
dtTable.Columns.Add(col);
DataRow drRow;
Microsoft.Office.Interop.Excel.Application ExcelObj =
new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook theWorkbook =
ExcelObj.Workbooks.Open(txt_FilePath.Text, 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);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
try
{
for (int sht = 1; sht <= sheets.Count; sht++)
{
Microsoft.Office.Interop.Excel.Worksheet worksheet =
(Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(sht);
for (int i = 2; i <= worksheet.UsedRange.Rows.Count; i++)
{
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "B" + i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value2;
String name = Convert.ToString(myvalues.GetValue(1, 1));
if (string.IsNullOrEmpty(name) == false)
{
drRow = dtTable.NewRow();
drRow["Rfid"] = name;
dtTable.Rows.Add(drRow);
}
}
Marshal.ReleaseComObject(worksheet);
worksheet = null;
}
return dtTable;
}
catch
{
throw;
}
finally
{
// Marshal.ReleaseComObject(worksheet);
Marshal.ReleaseComObject(sheets);
Marshal.ReleaseComObject(theWorkbook);
Marshal.ReleaseComObject(ExcelObj);
//worksheet = null;
sheets = null;
theWorkbook = null;
ExcelObj = null;
}
Microsoft has a built in solution for Importing/Exporting Excel files. It's not the most straightforward library but it generally works better than the others listed above.
The library required to do this is included in Office and can be found under the list of Framework assemblies at Microsoft.Office.Interop.Excel.
Here is some example code:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application app = new Excel.Application();
//Open existing workbook
//Excel.Workbook workbook = xlApp.Workbooks.Open(fileName);
//Create new workbook
Excel.Workbook workbook = app.Workbooks.Add();
Excel.Worksheet worksheet = workbook.ActiveSheet;
worksheet.Cells[1,1] = "Hello world!"; // Indexes start at 1, because Excel
workbook.SaveAs("C:\\MyWorkbook.xlsx");
workbook.Close();
app.Quit();

Categories

Resources