如何使用C#创建Excel电子表格而不需要在运行代码的计算机上安装Excel?


当前回答

从C#创建Excel文件的最简单和最快捷的方法是使用OpenXMLProductivity工具。OpenXMLProductivity工具随OpenXMLSDK安装一起提供。该工具将任何Excel文件反向工程为C#代码。然后可以使用C#代码重新生成该文件。

所涉及的流程概述如下:

使用该工具安装OpenXMLSDK。使用具有所需外观的最新Excel客户端创建Excel文件。将其命名为DesiredLook.xlsx。使用该工具打开DesiredLook.xlsx并单击顶部附近的Reflect Code按钮。文件的C#代码将在工具的右窗格中生成。将其添加到C#解决方案中,并生成具有所需外观的文件。

另外,此方法适用于任何Word和PowerPoint文件。作为C#开发人员,您将根据需要对代码进行更改。

我在github上开发了一个简单的WPF应用程序,将在Windows上运行。有一个名为GeneratedClass的占位符类,您可以在其中粘贴生成的代码。如果您返回文件的一个版本,它将生成如下excel文件:

其他回答

如果您正在创建Excel 2007/2010文件,请尝试此开源项目:https://github.com/closedxml/closedxml

它提供了一种面向对象的方式来操作文件(类似于VBA),而无需处理XML文档的麻烦。可以使用使用任何.NET语言,如C#和Visual Basic(VB)。ClosedXML允许您在不使用Excel的情况下创建Excel 2007/2010文件应用典型的示例是在web上创建Excel报表服务器:var工作簿=新XLWorkbook();var worksheet=workbook.Worksheets.Add(“样本表”);worksheet.Cell(“A1”).Value=“Hello World!”;工作簿.另存为(“HelloWorld.xlsx”);

public class GridViewExportUtil
{
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }
}

嗨,这个解决方案是将网格视图导出到excel文件中,这可能会帮助您

您可以使用ExcelXmlWriter。

它工作得很好。

我同意生成XML电子表格,这里有一个关于如何在C#中实现它的示例(每个人都在VB9:P中写博客)http://www.aaron-powell.com/linq-to-xml-to-excel

你可能想看看GemBox.Spresheet。

他们有一个免费版本,具有所有功能,但如果符合您的需要,则限制为每张150行,每张工作簿5页。

我还不需要自己使用它,但看起来很有趣。