如何使用C#创建Excel电子表格而不需要在运行代码的计算机上安装Excel?
当前回答
只想添加另一个引用到直接解决您问题的第三方解决方案:http://www.officewriter.com
(免责声明:我为制作OfficeWriter的公司SoftArtisans工作)
其他回答
一个经常被忽略的简单选项是使用Microsoft Reporting创建.rdlc报告并将其导出为excel格式。您可以在visual studio中设计它,并使用以下方法生成文件:
localReport.Render("EXCELOPENXML", null, ((name, ext, encoding, mimeType, willSeek) => stream = new FileStream(name, FileMode.CreateNew)), out warnings);
您还可以分别使用“WORDOPENXML”和“pdf”将其导出为.doc或.pdf,并且它在许多不同的平台(如ASP.NET和SSRS)上都受支持。
在可视化设计器中进行更改要容易得多,在那里您可以看到结果,相信我,一旦开始分组数据、格式化组标题、添加新节,您就不想处理几十个XML节点。
实际上,您可能想查看C#中可用的互操作类(例如Microsoft.Office.interop.Excel)。您可以说没有OLE(这不是),但互操作类非常容易使用。在这里查看C#文档(Interop for Excel从C#PDF的第1072页开始)。
如果你没有尝试过,你可能会印象深刻。
请注意Microsoft对此的立场:
Microsoft当前不建议也不支持,从任何无人值守、,非交互式客户端应用程序或组件(包括ASP,ASP.NET、DCOM和NT服务),因为Office可能表现出不稳定Office在此环境中运行时的行为和/或死锁。
您可以使用此库创建格式良好的Excel文件:http://officehelper.codeplex.com/documentation参见以下示例:
using (ExcelHelper helper = new ExcelHelper(TEMPLATE_FILE_NAME, GENERATED_FILE_NAME))
{
helper.Direction = ExcelHelper.DirectionType.TOP_TO_DOWN;
helper.CurrentSheetName = "Sheet1";
helper.CurrentPosition = new CellRef("C3");
//the template xlsx should contains the named range "header"; use the command "insert"/"name".
helper.InsertRange("header");
//the template xlsx should contains the named range "sample1";
//inside this range you should have cells with these values:
//<name> , <value> and <comment>, which will be replaced by the values from the getSample()
CellRangeTemplate sample1 = helper.CreateCellRangeTemplate("sample1", new List<string> {"name", "value", "comment"});
helper.InsertRange(sample1, getSample());
//you could use here other named ranges to insert new cells and call InsertRange as many times you want,
//it will be copied one after another;
//even you can change direction or the current cell/sheet before you insert
//typically you put all your "template ranges" (the names) on the same sheet and then you just delete it
helper.DeleteSheet("Sheet3");
}
示例如下:
private IEnumerable<List<object>> getSample()
{
var random = new Random();
for (int loop = 0; loop < 3000; loop++)
{
yield return new List<object> {"test", DateTime.Now.AddDays(random.NextDouble()*100 - 50), loop};
}
}
您可以使用ExcelXmlWriter。
它工作得很好。
从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文件:
推荐文章
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头
- 如果我使用OWIN Startup.cs类并将所有配置移动到那里,我是否需要一个Global.asax.cs文件?
- VS2013外部构建错误"error MSB4019: The imported project <path> was not found"
- 从另一个列表id中排序一个列表
- 等待一个无效的异步方法
- 无法加载文件或程序集…参数不正确
- c#中枚举中的方法
- 如何从字符串中删除新的行字符?
- 如何设置一个默认值与Html.TextBoxFor?
- 检查属性是否有属性
- 格式化XML字符串以打印友好的XML字符串
- 返回内容与IHttpActionResult非ok响应
- 从IEnumerable<KeyValuePair<>>重新创建字典
- c#测试用户是否有对文件夹的写权限