创建使用SAX OpenXmlWriter风格标签Excel文件 [英] Create Excel file with style tag using OpenXmlWriter SAX

查看:373
本文介绍了创建使用SAX OpenXmlWriter风格标签Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个Excel XLSX表格使用OpenXmlWriter(SAX)造型。



我能够创建一个行和列(填充它们作为文件字符串)。
我在寻找如何使第一行(头),以大胆的字体的简单的代码。



我没有一个模板文件开始因为该文件将是动态的。



我发现了如何添加WorkbookStylesPart几篇文章,但他们使用的DOM所有。因为我需要写大量的行,则DOM不会为我工作。



任何人都可以点我在正确的方向吗?
简单的代码中使用WriteStartElement和OpenXmlAttribute时添加标题行大胆。



谢谢,odansky


解决方案

添加stylesheet是一次性的工作。之后,你只需要创建新的细胞时,仅仅指的定义的样式ID。



热到样式表添加到电子表格,以大胆文字样式]



 私人WorkbookStylesPart的addStylesheet(SpreadsheetDocument电子表格)
{
WorkbookStylesPart样式表= spreadsheet.WorkbookPart.AddNewPart< WorkbookStylesPart>();

样式workbookstylesheet =新样式表();

字体FONT0 =新字体(); //默认字体

字体font1 =新字体(); //粗体
黑体加粗=新款Bold();
font1.Append(黑体);

字体的字体=新字体(); //< APENDING字体>
fonts.Append(FONT0);
fonts.Append(font1);

//<&填充GT;
填充fill0 =新的填充(); //默认填充

填充填充=新的填充(); //< APENDING填充>
fills.Append(fill0);

//<边框和GT;
边境border0 =新的边界(); // Defualt边境

国界边境=新的边界(); //< APENDING边框和GT;
borders.Append(border0);

//< CellFormats>
CellFormat cellformat0 =新CellFormat(){fontid代表= 0,FillId = 0,BorderId = 0}; //默认样式:强制|样式ID = 0

CellFormat cellformat1 =新CellFormat(fontid代表= 1)//风格大胆的文字;样式ID = 1


//< APENDING CellFormats>
CellFormats cellformats =新CellFormats();
cellformats.Append(cellformat0);
cellformats.Append(cellformat1);


//附加字体,填充,边框和放大器; CellFormats到样式表<保持排序>
workbookstylesheet.Append(字体);
workbookstylesheet.Append(填充);
workbookstylesheet.Append(边界);
workbookstylesheet.Append(cellformats);

//敲定
stylesheet.Stylesheet = workbookstylesheet;
stylesheet.Stylesheet.Save();

返回样式表;
}

现在,当您创建一个单元格做以下指的粗体字

 单元格C1 =新的细胞(){StyleIndex = Convert.ToUInt32(1)}; //指定我们的粗体字定义的样式;样式ID 1 



附加说明:您需要添加电子表格的workbookpart后添加的样式表。

更多关于SAX的方法:你可以定义样式当你第一次创建你要去打开要插入数据的单元格模板文件。当添加数据的单元格指使用标识的定义的样式。



一个简单的工作表格风格(的 MSDN

 公共静态无效CreateSpreadsheetWorkbook(字符串文件路径)
{
//通过提供文件路径创建一个电子表格文档。
//默认情况下,自动保存= true时,可编辑= TRUE,和Type = XLSX。
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(文件路径,SpreadsheetDocumentType.Workbook);
$ B $B∥加上一个WorkbookPart到文档。
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook =新的工作簿();

的addStylesheet(spreadsheetDocument)//< ==使用上述函数

//添加WorksheetPart到WorkbookPart添加样式表。
WorksheetPart worksheetPart = workbookpart.AddNewPart< WorksheetPart>();
worksheetPart.Worksheet =新的工作表(新SheetData());

//添加表到工作簿。
表表= spreadsheetDocument.WorkbookPart.Workbook.AppendChild<表及GT;(新表());

//附加一个新的工作表,并将其与该工作簿关联。
表表=新表(){ID = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),SheetId = 1,名称=MySheet的工作};
sheets.Append(片);

workbookpart.Workbook.Save();

//关闭文档。
spreadsheetDocument.Close();
}


I am trying to write an Excel Xlsx spreadsheet with styling using OpenXmlWriter (SAX).

I am able to create the file with rows and columns (populate them as strings). I am looking for a simple code on how to make the first row (header) with bold font.

I do not have a template file to start with as the file will be dynamic.

I found a few articles on how to add WorkbookStylesPart, but they are all using the DOM. As i need to write large number of rows, the DOM will not work for me.

Could anyone point me at the right direction? Simple code to add a header row as bold when using WriteStartElement and OpenXmlAttribute.

Thanks, odansky

解决方案

Adding StyleSheet is one time work. After that you just need to simply refer the defined style ID when creating new cells.

Hot to add a stylesheet to spreadsheet [With Bold text style]

private WorkbookStylesPart AddStyleSheet(SpreadsheetDocument spreadsheet)
 {
       WorkbookStylesPart stylesheet = spreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();

       Stylesheet workbookstylesheet = new Stylesheet();

       Font font0 = new Font();         // Default font

       Font font1 = new Font();         // Bold font
       Bold bold = new Bold();
       font1.Append(bold);

       Fonts fonts = new Fonts();      // <APENDING Fonts>
       fonts.Append(font0);
       fonts.Append(font1);

       // <Fills>
       Fill fill0 = new Fill();        // Default fill

       Fills fills = new Fills();      // <APENDING Fills>
       fills.Append(fill0);

       // <Borders>
       Border border0 = new Border();     // Defualt border

       Borders borders = new Borders();    // <APENDING Borders>
       borders.Append(border0);

       // <CellFormats>
        CellFormat cellformat0 = new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 }; // Default style : Mandatory | Style ID =0

       CellFormat cellformat1 = new CellFormat(FontId = 1)  // Style with Bold text ; Style ID = 1


      // <APENDING CellFormats>
      CellFormats cellformats = new CellFormats();
      cellformats.Append(cellformat0);
      cellformats.Append(cellformat1);


       // Append FONTS, FILLS , BORDERS & CellFormats to stylesheet <Preserve the ORDER>
      workbookstylesheet.Append(fonts);
      workbookstylesheet.Append(fills);
      workbookstylesheet.Append(borders);
      workbookstylesheet.Append(cellformats);

      // Finalize
      stylesheet.Stylesheet = workbookstylesheet;
      stylesheet.Stylesheet.Save();

      return stylesheet;
}

Now when you create a cell do following to refer to the Bold text

Cell c1 = new Cell(){StyleIndex = Convert.ToUInt32(1)}; // Assign our defined style with Bold text ; Style ID 1

Additional Note : You need to add stylesheet after adding workbookpart of the spreadsheet.

More regarding SAX approach : You can define styles when you first create the template file which you gonna open to insert data cells. And when adding data cells refer to the defined styles using ID.

A simple working spreadsheet with style (MSDN)

public static void CreateSpreadsheetWorkbook(string filepath)
{
    // Create a spreadsheet document by supplying the filepath.
    // By default, AutoSave = true, Editable = true, and Type = xlsx.
    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

    // Add a WorkbookPart to the document.
    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

   AddStyleSheet(spreadsheetDocument) // <== Adding stylesheet using above function

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet(new SheetData());

    // Add Sheets to the Workbook.
    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

    // Append a new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
    sheets.Append(sheet);

    workbookpart.Workbook.Save();

    // Close the document.
    spreadsheetDocument.Close();
}

这篇关于创建使用SAX OpenXmlWriter风格标签Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆