如何用C#中的工作表创建一个excel文件?

问题描述:

我想创建一个excel文件说personinfo.xls与一张工作表说如何用C#中的工作表创建一个excel文件?

在C#中有没有办法做到这一点?

+0

这个MSDN文章在看看[如何:使用COM互操作创建一个Excel电子表格(C#编程指南) (HTTP:// msdn.microsoft.com/en-us/library/ms173186(VS.80).aspx) – Joe 2009-10-07 05:51:44

您是否尝试过Excel Interop?这是为Excel 2003/2007吗? 如果是Excel 2007中的XML,你可以尝试

Carlos

SpreadsheetGear

ExcelPackage (Codeplex)

+0

ExcelPackage(在CodePLex上)+1 - 出色的工具 – 2009-10-07 05:54:07

+0

+1来自Carlos的ExcelXmlWriter。工作得很好。 – 2009-10-07 15:51:13

你可以做到这一点与MyXLS

我已经在一些项目中使用它,取得了巨大的成功。

其开源。

试试这个类:

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(); 

     } 


    } 

} 
+0

为什么那些GC.Collect()调用? – 2009-10-07 06:57:15

+0

最后一个是因为清理COM对象时,.NET垃圾收集器不一定会自动运行;所以如果您尝试再次调用该函数,它可能会创建一个新的Excel实例。 第一个在那里只是为了调试目的 - 如果代码在最后一次运行中途失败,要摆脱任何挥之不去的Excels:0) – ScottF 2009-10-07 07:08:14

+0

这应该是你如何做到的,不要使用任何第三种党的图书馆。尽管必须确保最终的发布,但PIA确实很容易使用,请参阅http://support.microsoft.com/default.aspx?scid=kb;en-us;317109。 – 2009-10-15 09:31:38

将下面的代码做到这一点:

  // 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); 

你可以下载一个免费试用here,如果你想自己尝试一下。

声明:我自己的SpreadsheetGear LLC

如果你只需要与Excel 2007中兼容或更高版本,我会去的OpenXML SDK

查看this thread第一页的最后一篇文章,这是一个非常基本的例子。

SpreadsheetML在开始时可能会相当混乱,但是如果您必须定期生成办公文档,那么确实值得学习。您可以在openxmldeveloper.org

问候寻找资源
奥利弗Hanappi