代码之家  ›  专栏  ›  技术社区  ›  JAiro

使用OpenXml sdk 2.0创建Excel文档

  •  16
  • JAiro  · 技术社区  · 17 年前

    private static Cell CreateTextCell(string header, string text, UInt32Value index)
    {
        Cell c = new Cell();
        c.DataType = CellValues.InlineString;
        c.CellReference = header + index;
        InlineString inlineString = new InlineString();
        DocumentFormat.OpenXml.Spreadsheet.Text t = new DocumentFormat.OpenXml.Spreadsheet.Text();
        t.Text = text;
        inlineString.AppendChild(t);
        c.AppendChild(inlineString);
        return c;
    } 
    
    3 回复  |  直到 9 年前
        1
  •  19
  •   foson    17 年前

    new Stylesheet(
    new Fonts(
        new Font(
            new FontSize() { Val = 10D },
            new Color() { Theme = (UInt32Value)1U },
            new FontName() { Val = "Arial" },
            new FontFamilyNumbering() { Val = 2 })
    ) { Count = (UInt32Value)1U },
    new Fills(
        new Fill(
            new PatternFill() { PatternType = PatternValues.None }),
        new Fill(
            new PatternFill() { PatternType = PatternValues.Gray125 })
    ) { Count = (UInt32Value)2U },
    new Borders(...
    ...
    ...
    new CellFormats(
    new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }) { Count = (UInt32Value)1U }, ...
    

    我添加了一个大小为12的新字体和一个红色背景的新填充(索引值64),并添加了引用新字体和填充索引的新单元格格式。(确保也更新计数)

    new Stylesheet(
        new Fonts(
            new Font(
                new FontSize() { Val = 10D },
                new Color() { Theme = (UInt32Value)1U },
                new FontName() { Val = "Arial" },
                new FontFamilyNumbering() { Val = 2 }),
            new Font(
                new FontSize() { Val = 12D },
                new Color() { Theme = (UInt32Value)1U },
                new FontName() { Val = "Arial" },
                new FontFamilyNumbering() { Val = 2 })
                ) { Count = (UInt32Value)2U },
        new Fills(
            new Fill(
                new PatternFill() { PatternType = PatternValues.None }),
            new Fill(
                new PatternFill() { PatternType = PatternValues.Gray125 }),
            new Fill(
                new PatternFill() { PatternType = PatternValues.Solid, ForegroundColor = new ForegroundColor() { Rgb = "FFFF0000" }, BackgroundColor = new BackgroundColor() { Indexed = 64 } })
                ) { Count = (UInt32Value)3U },
        new Borders(
            new Border(
                new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder())
        ) { Count = (UInt32Value)1U },
        new CellStyleFormats(
            new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U }
        ) { Count = (UInt32Value)1U },
        new CellFormats(
            new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U },
            new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)1U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U },
            new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }
        ) { Count = (UInt32Value)3U },
        new CellStyles(
            new CellStyle() { Name = "Normal", FormatId = (UInt32Value)0U, BuiltinId = (UInt32Value)0U }
        ) { Count = (UInt32Value)1U },
        new DifferentialFormats() { Count = (UInt32Value)0U },
        new TableStyles() { Count = (UInt32Value)0U, DefaultTableStyle = "TableStyleMedium9", DefaultPivotStyle = "PivotStyleLight16" });
    

    SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
    sheetData.Descendants<Row>().Where(r => r.RowIndex == 2U).First().Descendants<Cell>().First().StyleIndex = 1U;
    sheetData.Descendants<Row>().Where(r => r.RowIndex == 3U).First().Descendants<Cell>().First().StyleIndex = 2U;
    
        2
  •  9
  •   Mike Gledhill    14 年前

    非常感谢这篇文章。

    经过很多努力(和谷歌搜索),我终于设法创建了一个

    DataSet ds = CreateSampleData();                  //  Your code here !
    string excelFilename = "C:\\Sample.xlsx";
    
    CreateExcelFile.CreateExcelDocument(ds, excelFilename);
    

    享受。

    http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

        3
  •  2
  •   Hailiang Wang    17 年前

    如何指定单元格样式?

    new Cell() { CellReference = "B6", StyleIndex = 11U }
    

    您不必按程序添加所有样式,而是可以创建一个包含所需所有格式的模板xlsx文件,然后在程序中指定样式索引。