如何在C#中创建带有工作表的Excel文件?

时间:2009-10-07 05:42:23

标签: c# openxml

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

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

5 个答案:

答案 0 :(得分:3)

您可以使用MyXLS执行此操作。

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

它的开源。

答案 1 :(得分:2)

您是否尝试过Excel Interop?这是for excel 2003/2007吗? 如果是excel 2007 xml,你可以尝试

Carlos

SpreadsheetGear

ExcelPackage (Codeplex)

答案 2 :(得分:2)

试试这堂课:

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

        }


    }

}

答案 3 :(得分:1)

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

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

SpreadsheetML在开始时可能会让人感到困惑,但如果你必须定期生成办公文档,那真的值得学习。您可以在openxmldeveloper.org

上找到资源

最诚挚的问候 Oliver Hanappi

答案 4 :(得分:0)

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