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


当前回答

我想知道为什么没有人建议PowerShell使用免费的ImportExcel模块;它可以轻松创建XML Excel文件(xlsx)。

从SQL Server等数据库创建Excel工作表时尤其容易。。。

其他回答

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

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文件中,这可能会帮助您

我使用了几个选项:

如果XLSX是必须的:ExcelPackage是一个很好的开始,但当开发人员停止工作时就夭折了。ExML不是一个坏选项,我仍在一些生产网站上使用它。

不过,对于我的所有新项目,我都使用NPOI,即Apache POI的.NET端口。NPOI 2.0(Alpha)也支持XLSX。

如果您对xlsx格式感到满意,请尝试我的库EPPlus。它从ExcelPackage的源代码开始,但后来变成了完全重写。

它支持范围、单元格样式、图表、形状、图片、命名范围、自动筛选以及许多其他功能。

您有两个选项:

EPPlus 4,根据LGPL许可(原始分支机构,开发至2020年)EPPlus 5,根据Polyform非商业1.0.0许可(自2020年起)。

从EPPlus 5自述文件:

有了新的许可证,EPPlus在某些情况下仍然可以免费使用,但需要商业许可证才能在商业企业中使用。

EPPlus网站:https://www.epplussoftware.com/

前段时间,我在NPOI上创建了一个DLL。使用它非常简单:

IList<DummyPerson> dummyPeople = new List<DummyPerson>();
//Add data to dummyPeople...
IExportEngine engine = new ExcelExportEngine();
engine.AddData(dummyPeople); 
MemoryStream memory = engine.Export();

你可以在这里阅读更多。

顺便说一句,它是100%开源的。随时使用、编辑和共享;)