I want to add to my application an ability to save data to XLS (old excel files).
I’ve seen an example on how to do this in ASP.NET here, but I don’t know how this translates into c# code for desktop applications.
I also had a look at excel automation, which looks like this:
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
And I don’t want to use it because:
-
It will be ueberslow if I export large amounts of data
-
I want this to work on PCs without Microsoft Excel installed if possible
I’ve also tried CarlosAg.Excel.Xml, and it seems to work, except that when I’m opening the file, I’m getting warning from excel, that the file is not in XLS format, but some other one.
Can anyone recommend to me a free c# library which would do this, or show me how to use conventional .Net libraries to save the data to XLS?
Use NOPI, which available on
codeplex.comHere’s an example on how to use NOPI:
Ref:
Creating Excel spreadsheets .XLS and .XLSX in C# by Leniel Macaferi .