C#,使用OpenXmlWriter和Open XML SDK编写XLSX

时间:2012-05-08 20:01:05

标签: c# export-to-excel openxml-sdk

在C#中,以下程序编译并运行,但它不会在excel输出文件中写入任何内容。

我让它在没有OpenXmlWriter的情况下工作但我开始耗尽内存所以我必须根据此http://blogs.msdn.com/b/brian_jones/archive/2010/06/22/writing-large-excel-files-with-the-open-xml-sdk.aspx

切换到OpenXmlWriter
class Program
{
    static void Main(string[] args)
    {
        File.Copy("book.xlsx", "output.xlsx", true);
        WriteValuesSAX("output.xlsx", 10, 10);
    }

    static void WriteValuesSAX(string filename, int numRows, int numCols)
    {
        using (SpreadsheetDocument myDoc = 
               SpreadsheetDocument.Open(filename, true))
        {
            WorkbookPart workbookPart = myDoc.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

            OpenXmlWriter writer = OpenXmlWriter.Create(worksheetPart);

            Row r = new Row();
            Cell c = new Cell();
            CellValue v = new CellValue("Test");
            c.AppendChild(v);

            writer.WriteStartElement(new SheetData());
            for (int row = 0; row < numRows; row++)
            {
                writer.WriteStartElement(r);
                for (int col = 0; col < numCols; col++)
                {
                    writer.WriteElement(c);
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();

            writer.Close();
        }
    }
}

为什么不写任何输出?

2 个答案:

答案 0 :(得分:2)

我还不太了解这个,但我确实有效。 这就是我最终的目标。

class Program
{
    static void Main(string[] args)
    {
        File.Copy("book.xlsx", "output.xlsx", true);
        WriteRandomValuesSAX("output.xlsx", 10, 10);
    }

    static void WriteRandomValuesSAX(string filename, int numRows, int numCols)
    {
        using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(filename, true))
        {
            WorkbookPart workbookPart = myDoc.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.Last();

            OpenXmlWriter writer = OpenXmlWriter.Create(worksheetPart);

            Row r = new Row();
            Cell c = new Cell();
            CellValue v = new CellValue("Test");
            c.AppendChild(v);

            writer.WriteStartElement(new Worksheet());
            writer.WriteStartElement(new SheetData());
            for (int row = 0; row < numRows; row++)
            {
                writer.WriteStartElement(r);
                for (int col = 0; col < numCols; col++)
                {
                    writer.WriteElement(c);
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.Close();
        }
    }
}

注意我添加了writer.WriteStartElement(new Worksheet());和另一个writer.WriteEndElement();


我在这里找到了正确的xml格式:http://msdn.microsoft.com/en-us/library/gg278309.aspx

哪个是

<?xml version="1.0" encoding="utf-8"?> <x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <x:sheetData>
        <x:row r="1">
            <x:c r="A1" t="n">
                <x:v>100</x:v>
            </x:c>
        </x:row>
    </x:sheetData> </x:worksheet>

所以我打开(解压缩)xlsx文件并导航到output.xlsx \ xl \ worksheets \ sheet1.xml,看到我错过了<x:worksheet>

答案 1 :(得分:0)

使用上面的代码给出了错误消息&#34; Excel找到了不可读的内容&#34;在Excel 2007中。 使用此处how-to-properly-use-openxmlwriter-to-write-large-excel-files中的信息并在将列添加到以下内容时更改代码会删除错误。

for(int col = 0; col < numCols; col++)
{
    var oxa = new List<OpenXmlAttribute>();
    oxa.Add(new OpenXmlAttribute("t",null,"str"));
    writer.WriteStartElement(c,oxa);
    writer.WriteElement(new CellValue(string.Format("R{0}C{1}", row, col)));
    writer.WriteEndElement();
}