【问题标题】:How to create a excel file with sheets in C#?如何在 C# 中创建带有工作表的 excel 文件?
【发布时间】:2013-02-11 05:03:38
【问题描述】:

我想创建一个 Excel 文件,比如 personinfo.xls,其中一张工作表说 Person

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

【问题讨论】:

标签: c# openxml


【解决方案1】:

您可以使用MyXLS 做到这一点。

我在几个项目中使用它并取得了巨大的成功。

它是开源的。

【讨论】:

    【解决方案2】:

    您是否尝试过 Excel 互操作?这是针对 excel 2003/2007 的吗? 如果是excel 2007 xml你可以试试

    Carlos

    SpreadsheetGear

    ExcelPackage (Codeplex)

    【讨论】:

      【解决方案3】:

      试试这个课程:

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

      【讨论】:

      • 为什么那些 GC.Collect() 调用??
      • 最后一个是因为.NET垃圾收集器在清除COM对象时不一定会自动运行;因此,如果您再次尝试调用该函数,它可能会创建一个新的 Excel 实例。第一个只是为了调试目的 - 以防代码在最后一次运行中途失败,以摆脱任何挥之不去的 Excel :0)
      • 这应该是你的做法,不要使用任何类型的第三方库。 PIA 真的很容易使用,尽管你必须确保最后发布看到support.microsoft.com/default.aspx?scid=kb;en-us;317109
      • 我认为第三方库有一个地方(特别是如果您的 C# 程序正在做大量 Excel 工作,因为一段时间后 Interops 可能会有点混乱 - 并且使用 Interops 进行排序是太痛苦了)——但我认为很多人只想要一点 Excel——在我们的例子中,它只是导出一个列表,使用整个 3rd 方库会给程序增加相当多的膨胀。
      【解决方案4】:

      如果您只需要与 Excel 2007 或更高版本兼容,我会选择OpenXML SDK

      查看this thread第一页的最后一篇文章,了解一个非常基本的示例。

      SpreadsheetML 刚开始可能会比较混乱,但如果您必须定期生成办公文档,它确实值得学习。你可以在openxmldeveloper.org找到资源

      最好的问候
      奥利弗·哈纳皮

      【讨论】:

        【解决方案5】:

        SpreadsheetGear for .NET 将使用以下代码:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-07-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多