我有一个使用 OpenXMLWriter 的问题要问。
我目前正在使用下面的代码来创建我的 excel 文件,但我想设置列的宽度。我该怎么做?
因为我试图在 Worksheet() 和 SheetData() 之间编写新的列,但到目前为止我还没有成功。
示例将非常有帮助。欣赏并感谢!
using (SpreadsheetDocument xl = SpreadsheetDocument.Create("LargeFile.xlsx", SpreadsheetDocumentType.Workbook))
{
List<OpenXmlAttribute> oxa;
OpenXmlWriter oxw;
xl.AddWorkbookPart();
WorksheetPart wsp = xl.WorkbookPart.AddNewPart<WorksheetPart>();
oxw = OpenXmlWriter.Create(wsp);
oxw.WriteStartElement(new Worksheet());
oxw.WriteStartElement(new SheetData());
for (int i = 1; i <= 50000; ++i)
{
oxa = new List<OpenXmlAttribute>();
// this is the row index
oxa.Add(new OpenXmlAttribute("r", null, i.ToString()));
oxw.WriteStartElement(new Row(), oxa);
for (int j = 1; j <= 100; ++j)
{
oxa = new List<OpenXmlAttribute>();
// this is the data type ("t"), with CellValues.String ("str")
oxa.Add(new OpenXmlAttribute("t", null, "str"));
// it's suggested you also have the cell reference, but
// you'll have to calculate the correct cell reference yourself.
// Here's an example:
//oxa.Add(new OpenXmlAttribute("r", null, "A1"));
oxw.WriteStartElement(new Cell(), oxa);
oxw.WriteElement(new CellValue(string.Format("R{0}C{1}", i, j)));
// this is for Cell
oxw.WriteEndElement();
}
// this is for Row
oxw.WriteEndElement();
}