tags:

views:

444

answers:

9

Hi All,

I want to create an excel file say personinfo.xls with one work sheet say "Person".

Is there a way in C# to do it, please suggest.

A: 

download this Openxml SDK

I have never used it before, but give it a try.

Andrew Keith
+2  A: 

Have you tried the Excel Interop? Is this for excel 2003/2007? If it is excel 2007 xml you can try

Carlos

SpreadsheetGear

ExcelPackage (Codeplex)

astander
+1 for the ExcelPackage (on CodePLex) - excellent tool
marc_s
+1 for ExcelXmlWriter from Carlos. Works quite well.
Ryan Rinaldi
A: 

Take a look at this MSDN article How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide)

Joe
+2  A: 

You can do that with MyXLS.

I have used it in a few project with great success.

Its opensource.

Allan Simonsen
+2  A: 

Try this class:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;

namespace ExcelDemo
{
    static class ExcelImportExport
    {
        public static void ExportToExcel(string strFileName, string strSheetName)
        {
            // Run the garbage collector
            GC.Collect();

            // Delete the file if it already exists
            if (System.IO.File.Exists(strFileName))
            {
                System.IO.File.SetAttributes(strFileName, FileAttributes.Normal);
                System.IO.File.Delete(strFileName);
            }

            // Open an instance of excel. Create a new workbook.
            // A workbook by default has three sheets, so if you just want a single one, delete sheet 2 and 3
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Excel._Workbook xlWB = (Excel._Workbook)xlApp.Workbooks.Add(Missing.Value);
            Excel._Worksheet xlSheet = (Excel._Worksheet)xlWB.Sheets[1];
            ((Excel._Worksheet)xlWB.Sheets[2]).Delete();
            ((Excel._Worksheet)xlWB.Sheets[2]).Delete();

            xlSheet.Name = strSheetName;
            // Write a value into A1
            xlSheet.Cells[1, 1] = "Some value";

            // Tell Excel to save your spreadsheet
            xlWB.SaveAs(strFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            xlApp.Quit();

            // Release the COM object, set the Excel variables to Null, and tell the Garbage Collector to do its thing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

            xlSheet = null;
            xlWB = null;
            xlApp = null;

            GC.Collect();

        }


    }

}
ScottF
why those GC.Collect() calls??
marc_s
The one at the end is because the .NET garbage collector won't necessarily automatically run when clearing COM objects; so if you try to call the function a second time, it might create a new instance of Excel.The first is in there just for debugging purposes - in case the code failed halfway through the last run, to get rid of any lingering Excels :0)
ScottF
This should be how you do it, don't use any kind of third party libraries. The PIAs are really easy to use though you have to make sure of the releasing at the end see http://support.microsoft.com/default.aspx?scid=kb;en-us;317109 .
PintSizedCat
I think there is a place for third party libraries (especially if your C# program is doing a lot of Excel work, because the Interops can be a bit messy after a while - and sorting with the Interops is so frickin' painful) - but I think a lot of people just want a little bit of Excel - in our case it is just exporting a list, and using a whole 3rd party library would add a fair chunk of bloat to the program.
ScottF
A: 

Try the link Click here

Himadri
A: 

SpreadsheetGear for .NET will do it with the following code:

            // Create a new empty workbook with one worksheet.
            IWorkbook workbook = Factory.GetWorkbook();
            // Get the worksheet and change it's name to "Person".
            IWorksheet worksheet = workbook.Worksheets[0];
            worksheet.Name = "Person";
            // Put "Hello World!" into A1.
            worksheet.Cells["A1"].Value = "Hello World!";
            // Save the workbook as xls (Excel 97-2003 / Biff8).
            workbook.SaveAs(@"C:\tmp\personinfo.xls", FileFormat.Excel8);
            // Save the workbook as xlsx (Excel 2007 Open XML).
            workbook.SaveAs(@"C:\tmp\personinfo.xlsx", FileFormat.OpenXMLWorkbook);

You can download a free trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
A: 

Hi!

If you need only to be compatible with Excel 2007 or later, I would go for the OpenXML SDK.

Check out the last post of the first page of this thread for a very basic example.

SpreadsheetML can be quite confusing in the beginning, but if you have to generate office documents regularly, it is really worth learing. You can find resources on openxmldeveloper.org

Best Regards
Oliver Hanappi

Oliver Hanappi