简体   繁体   English

在C#中格式化Excel工作表

[英]format excel sheet in c#

我想格式化我的c#应用程序生成的excel工作表,按照模板excel /源excel工作表的格式。

I have done this using Linq to XML and the SpreadsheetML format used when you save an Excel workbook as XML. 我已经使用Linq to XML和将Excel工作簿另存为XML时使用的SpreadsheetML格式完成了此操作。 Basically, you create the format spreadsheet however you need it. 基本上,您可以创建格式电子表格,但是需要它。 Then, you save it as XML. 然后,将其另存为XML。 You find the areas that you want to programatically alter, and use Linq to XML to do so. 您找到要以编程方式更改的区域,然后使用Linq to XML进行更改。 The easiest way to identify interesting areas in the spreadsheet is to use labelled cells. 识别电子表格中有趣区域的最简单方法是使用标记的单元格。

The SpreadsheetML can get pretty complicated, but this method has worked for me in the past. SpreadsheetML可能会变得非常复杂,但是该方法在过去对我有用。

Using the Interop.Excel assembly ( How to ), you can load your excel template when creating the new Workbook: 使用Interop.Excel程序集方法 ),可以在创建新工作簿时加载excel模板:

Microsoft.Office.Interop.Excel.Application xlApp = new Application();
Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Open("excelTemplateFile.xls",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM