作者:冲动王子2502901503 | 来源:互联网 | 2023-05-21 16:14
在使用OpenXml生成.xlsx文件时,如何设置要包装的文本单元格的格式?这是我目前的代码:
public void Excel()
{
var viewModel = new RequirementIndexData();
viewModel.Requirements = db.Requirement;
MemoryStream ms = new MemoryStream();
SpreadsheetDocument dc = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook);
WorkbookPart mdp = dc.AddWorkbookPart();
WorksheetPart wsp = mdp.AddNewPart();
Workbook wb = new Workbook();
FileVersion fv = new FileVersion();
fv.ApplicatiOnName= "Microsoft Office Excel";
Worksheet ws = new Worksheet();
SheetData sd = new SheetData();
Columns columns = new Columns();
columns.Append(CreateColumnData(1, 1, 95));
ws.Append(columns);
Row r1 = new Row() { RowIndex = (UInt32Value)1u };
Cell c1 = new Cell();
c1.DataType = CellValues.SharedString;
c1.CellValue = new CellValue(OpenSoft.AgileAnalytics.EF.App_LocalResources.GlobalRes.VersionNumber + " " + viewModel.Requirements.OrderBy(r => r.CreatedOn).Select(r => r.CurentVersion).First().ToString());
r1.Append(c1);
sd.Append(r1);
for (int i = 2; i r.CreatedOn).Select(r => r.Definition).ElementAt(i - 2).ToString());
row2.Append(cell2);
sd.Append(row2);
}
ws.Append(sd);
wsp.Worksheet = ws;
wsp.Worksheet.Save();
Sheets sheets = new Sheets();
Sheet sheet = new Sheet();
sheet.Name = "specification1";
sheet.SheetId = 1;
sheet.Id = mdp.GetIdOfPart(wsp);
sheets.Append(sheet);
wb.Append(fv);
wb.Append(sheets);
dc.WorkbookPart.Workbook = wb;
dc.WorkbookPart.Workbook.Save();
dc.Close();
string filename = "specification1.xlsx";
Response.Clear();
byte[] dt = ms.ToArray();
Response.COntentType= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
Response.BinaryWrite(dt);
Response.End();
}
private WorkbookStylesPart AddStyleSheet(SpreadsheetDocument spreadsheet)
{
WorkbookStylesPart stylesheet = spreadsheet.WorkbookPart.AddNewPart();
Stylesheet workbookstylesheet = new Stylesheet();
//
Font font0 = new Font(); // Default font
Fonts fOnts= new Fonts(); //
fonts.Append(font0);
//
Fill fill0 = new Fill(); // Default fill
Fills fills = new Fills(); //
fills.Append(fill0);
//
Border border0 = new Border(); // Defualt border
Borders borders = new Borders(); //
borders.Append(border0);
//
CellFormat cellformat0 = new CellFormat() { FOntId= 0, FillId = 0, BorderId = 0 }; // Default style : Mandatory
CellFormat cellformat1 = new CellFormat(new Alignment() { WrapText = true }); // Style with textwrap set
//
CellFormats cellformats = new CellFormats();
cellformats.Append(cellformat0);
cellformats.Append(cellformat1);
// Append FONTS, FILLS , BORDERS & CellFormats to stylesheet
workbookstylesheet.Append(fonts);
workbookstylesheet.Append(fills);
workbookstylesheet.Append(borders);
workbookstylesheet.Append(cellformats);
// Finalize
stylesheet.Stylesheet = workbookstylesheet;
stylesheet.Stylesheet.Save();
return stylesheet;
}
Kavindu Doda..
14
您需要为此定义样式.样式在样式表中定义.每种样式都有一个ID,当您创建单元格时,您可以引用定义的样式ID.
将样式表定义为电子表格:
private WorkbookStylesPart AddStyleSheet(SpreadsheetDocument spreadsheet)
{
WorkbookStylesPart stylesheet = spreadsheet.WorkbookPart.AddNewPart();
Stylesheet workbookstylesheet = new Stylesheet();
//
Font font0 = new Font(); // Default font
Fonts fOnts= new Fonts(); //
fonts.Append(font0);
//
Fill fill0 = new Fill(); // Default fill
Fills fills = new Fills(); //
fills.Append(fill0);
//
Border border0 = new Border(); // Defualt border
Borders borders = new Borders(); //
borders.Append(border0);
//
CellFormat cellformat0 = new CellFormat() // Default style : Mandatory
{
FOntId= 0,
FillId = 0,
BorderId = 0
};
CellFormat cellformat1 = new CellFormat(new Alignment() { WrapText = true }); // Style with textwrap set
//
CellFormats cellformats = new CellFormats();
cellformats.Append(cellformat0);
cellformats.Append(cellformat1);
// Append FONTS, FILLS , BORDERS & CellFormats to stylesheet
workbookstylesheet.Append(fonts);
workbookstylesheet.Append(fills);
workbookstylesheet.Append(borders);
workbookstylesheet.Append(cellformats);
// Finalize
stylesheet.Stylesheet = workbookstylesheet;
stylesheet.Stylesheet.Save();
return stylesheet;
}
现在,当您添加单元格时,请使用define样式ID,如下所示:
// Assign our defined style with text wrap.
Cell c1 = new Cell(){ StyleIndex = Convert.ToUInt32(1) };
编辑:您需要在添加workbookpart后添加样式表..
1> Kavindu Doda..:
您需要为此定义样式.样式在样式表中定义.每种样式都有一个ID,当您创建单元格时,您可以引用定义的样式ID.
将样式表定义为电子表格:
private WorkbookStylesPart AddStyleSheet(SpreadsheetDocument spreadsheet)
{
WorkbookStylesPart stylesheet = spreadsheet.WorkbookPart.AddNewPart();
Stylesheet workbookstylesheet = new Stylesheet();
//
Font font0 = new Font(); // Default font
Fonts fOnts= new Fonts(); //
fonts.Append(font0);
//
Fill fill0 = new Fill(); // Default fill
Fills fills = new Fills(); //
fills.Append(fill0);
//
Border border0 = new Border(); // Defualt border
Borders borders = new Borders(); //
borders.Append(border0);
//
CellFormat cellformat0 = new CellFormat() // Default style : Mandatory
{
FOntId= 0,
FillId = 0,
BorderId = 0
};
CellFormat cellformat1 = new CellFormat(new Alignment() { WrapText = true }); // Style with textwrap set
//
CellFormats cellformats = new CellFormats();
cellformats.Append(cellformat0);
cellformats.Append(cellformat1);
// Append FONTS, FILLS , BORDERS & CellFormats to stylesheet
workbookstylesheet.Append(fonts);
workbookstylesheet.Append(fills);
workbookstylesheet.Append(borders);
workbookstylesheet.Append(cellformats);
// Finalize
stylesheet.Stylesheet = workbookstylesheet;
stylesheet.Stylesheet.Save();
return stylesheet;
}
现在,当您添加单元格时,请使用define样式ID,如下所示:
// Assign our defined style with text wrap.
Cell c1 = new Cell(){ StyleIndex = Convert.ToUInt32(1) };
编辑:您需要在添加workbookpart后添加样式表..