Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用C#创建包含工作表的excel文件?_C#_Openxml - Fatal编程技术网

如何用C#创建包含工作表的excel文件?

如何用C#创建包含工作表的excel文件?,c#,openxml,C#,Openxml,我想创建一个excel文件,比如说personinfo.xls,其中一个工作表是Person C语言中有什么方法可以做到这一点吗?您尝试过Excel互操作吗?这是excel 2003/2007的吗? 如果是Excel2007XML,您可以试试 您可以使用 我已经在一些项目中使用了它,并取得了巨大的成功 它是开源的。试试这个类: using System; using System.Collections.Generic; using System.Text; using System.IO;

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


C语言中有什么方法可以做到这一点吗?

您尝试过Excel互操作吗?这是excel 2003/2007的吗? 如果是Excel2007XML,您可以试试

您可以使用

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

它是开源的。

试试这个类:

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

        }


    }

}
将使用以下代码执行此操作:

            // 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);
如果你想亲自试用,你可以下载免费试用版


免责声明:我拥有SpreadsheetGear LLC

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

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

SpreadsheetML在一开始可能会很混乱,但是如果您必须定期生成office文档,那么它确实值得学习。您可以在上找到资源

致以最诚挚的问候

Oliver Hanappi

为什么要调用那些GC.Collect()调用??最后的调用是因为.NET垃圾收集器在清除COM对象时不一定会自动运行;因此,如果您再次尝试调用该函数,它可能会创建一个新的Excel实例。第一个只是为了调试目的——如果代码在上一次运行的中途失败,为了消除任何挥之不去的优点:0)应该这样做,不要使用任何第三方库。PIA真的很容易使用,尽管你必须确保最终的发布,请参见。我认为第三方库是有位置的(特别是如果你的C#程序正在做大量的Excel工作,因为Interops可能会在一段时间后变得有点凌乱,而使用Interops进行排序非常痛苦)-但我认为很多人只是想要一点Excel-在我们的例子中,这只是导出一个列表,使用整个第三方库会给程序增加相当多的膨胀。看看这篇MSDN文章[如何:使用COM Interop创建Excel电子表格(C#编程指南)]()