以下内容采用现有工作表sSheet
的格式,并在A19处创建红色字体(以及一些时间戳内容):
using (SpreadsheetDocument document = SpreadsheetDocument.Open(sPath, true)) {
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sSheet);
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.FirstOrDefault().Id);
Worksheet worksheet = worksheetPart.Worksheet;
WorkbookStylesPart styles = document.WorkbookPart.WorkbookStylesPart;
Stylesheet stylesheet = styles.Stylesheet;
CellFormats cellformats = stylesheet.CellFormats;
Fonts fonts = stylesheet.Fonts;
UInt32 fontIndex = fonts.Count;
UInt32 formatIndex = cellformats.Count;
Cell cell = GetCell(worksheet, "A", 19);
cell.CellValue = new CellValue(DateTime.Now.ToLongTimeString());
cell.DataType = new EnumValue<CellValues>(CellValues.String);
CellFormat f = (CellFormat)cellformats.ElementAt((int)cell.StyleIndex.Value);
var font = (Font)fonts.ElementAt((int)f.FontId.Value);
var newfont = (Font)font.Clone();
newfont.Color = new Color() { Rgb = new HexBinaryValue("ff0000") };
fonts.Append(newfont);
CellFormat newformat = (CellFormat)f.Clone();
newformat.FontId = fontIndex;
cellformats.Append(newformat);
stylesheet.Save();
cell.StyleIndex = formatIndex;
worksheetPart.Worksheet.Save();
document.WorkbookPart.Workbook.Save();
document.Close();
}
但是我不能使它产生实心填充。我尝试添加
Fills fills = stylesheet.Fills;
下的Fonts fonts = stylesheet.Fonts;
,以及在
var font = (Font)fonts.ElementAt((int)f.FontId.Value);
var newfont = (Font)font.Clone();
newfont.Color = new Color() { Rgb = new HexBinaryValue("ff0000") };
fonts.Append(newfont);
...以及它的许多变体,但未编译。我不知道如何添加纯色填充。任何帮助深表感谢。谢谢。
答案 0 :(得分:1)
我获得了下面的代码,方法是创建两个基本的“空”工作簿,一个工作簿具有具有背景色的单元格,另一个工作簿具有背景色。然后,我使用Open XML SDK生产率工具打开了后者,并使用它与具有背景填充的工作簿进行了比较。这样就产生了将一个变成另一个的代码。
如您所见,有必要使用Fill
,PatternFill
和ForegroundColor BackgroundColor
CellFormats`创建一个新的elements. This is added to the list of
,以便它可以被单元格上的引用工作表。
private void btnAddFillColor_Click(object sender, EventArgs e)
{
string filePath = "C:\\Test\\OpenXMLTest_NoFillColor.xlsx";
using (SpreadsheetDocument document = SpreadsheetDocument.Open(filePath, true))
{
WorkbookPart wbPart = document.WorkbookPart;
WorksheetPart wsPart = wbPart.WorksheetParts.FirstOrDefault();
WorkbookStylesPart stylesPart = document.WorkbookPart.WorkbookStylesPart;
ChangeWorkbookStylesPart(stylesPart);
ChangeWorksheetPart(wsPart);
}
}
private void ChangeWorkbookStylesPart(WorkbookStylesPart workbookStylesPart1)
{
xl.Stylesheet stylesheet1 = workbookStylesPart1.Stylesheet;
xl.Fills fills1 = stylesheet1.GetFirstChild<xl.Fills>();
xl.CellFormats cellFormats1 = stylesheet1.GetFirstChild<xl.CellFormats>();
fills1.Count = (UInt32Value)3U;
xl.Fill fill1 = new xl.Fill();
xl.PatternFill patternFill1 = new xl.PatternFill() { PatternType = xl.PatternValues.Solid };
xl.ForegroundColor foregroundColor1 = new xl.ForegroundColor() { Rgb = "FFC00000" };
xl.BackgroundColor backgroundColor1 = new xl.BackgroundColor() { Indexed = (UInt32Value)64U };
patternFill1.Append(foregroundColor1);
patternFill1.Append(backgroundColor1);
fill1.Append(patternFill1);
fills1.Append(fill1);
cellFormats1.Count = (UInt32Value)2U;
xl.CellFormat cellFormat1 = new xl.CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyFill = true };
cellFormats1.Append(cellFormat1);
}
private void ChangeWorksheetPart(WorksheetPart worksheetPart1)
{
xl.Worksheet worksheet1 = worksheetPart1.Worksheet;
xl.SheetData sheetData1 = worksheet1.GetFirstChild<xl.SheetData>();
xl.Row row1 = sheetData1.GetFirstChild<xl.Row>();
xl.Cell cell1 = row1.GetFirstChild<xl.Cell>();
cell1.StyleIndex = (UInt32Value)1U;
}
答案 1 :(得分:0)
通过调用ExportDataSetToExcellWithColour_openXML(your_dataset,your_destination_file_full_path.xlsx)生成xlsx文件。如果该文件存在,它将首先被删除。
除OpenXML外,不使用其他第三方库。
您的数据表应类似于:
列a,列b,列c,颜色
abc,def,ghi,#FF0000
aa1,bb2,ccc,#FFFF00
“颜色”列的数据应为十六进制颜色代码。在目标xlsx文件中将删除“颜色”列,并以指定的颜色绘制每一行。
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
......
public static void ExportDataSetToExcellWithColour_openXML(DataSet ds, string destination)
{
if (File.Exists(destination))
{
try
{
File.SetAttributes(destination, FileAttributes.Normal);
File.Delete(destination);
}
catch { }
}
using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
#region deal with color
List<string> colorCode = new List<string>();
foreach (global::System.Data.DataTable table in ds.Tables)
{
if (table.Columns.Contains("color"))
{
foreach (DataRow dr in table.Rows)
{
if (!colorCode.Contains(dr["color"].ToString()))
{
colorCode.Add(dr["color"].ToString());
}
}
}
}
WorkbookStylesPart stylePart = workbook.WorkbookPart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = CreateStylesheet(colorCode);
//stylePart.Stylesheet = CreateStylesheet_ori();
stylePart.Stylesheet.Save();
#endregion
foreach (global::System.Data.DataTable table in ds.Tables)
{
DataTable ori_dt = null;
if (table.Columns.Contains("color"))
{
ori_dt = table.Clone();
foreach (DataRow dr in table.Rows)
{
ori_dt.Rows.Add(dr.ItemArray);
}
table.Columns.Remove("color");
}
var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
uint sheetId = 1;
if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
{
sheetId =
sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
sheets.Append(sheet);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<String> columns = new List<string>();
foreach (global::System.Data.DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
int rowindex = 0;
foreach (global::System.Data.DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach (String col in columns)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
if (ori_dt.Columns.Contains("color"))
{
try
{
cell.StyleIndex =
(
(UInt32)colorCode.FindIndex(
a => a == ori_dt.Rows[rowindex]["color"].ToString()
)
) + 1;
//cell.StyleIndex = (UInt32)2;
}
catch (Exception ex)
{
}
}
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
rowindex++;
}
}
}
}
public static Stylesheet CreateStylesheet(List<string> colorCode)
{
Stylesheet stylesheet1 = new Stylesheet() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x14ac" } };
stylesheet1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
stylesheet1.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
DocumentFormat.OpenXml.Spreadsheet.Fonts fonts1 = new DocumentFormat.OpenXml.Spreadsheet.Fonts() { Count = (UInt32Value)1U, KnownFonts = true };
DocumentFormat.OpenXml.Spreadsheet.Font font1 = new DocumentFormat.OpenXml.Spreadsheet.Font();
DocumentFormat.OpenXml.Spreadsheet.FontSize fontSize1 = new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11D };
DocumentFormat.OpenXml.Spreadsheet.Color color1 = new DocumentFormat.OpenXml.Spreadsheet.Color() { Theme = (UInt32Value)1U };
DocumentFormat.OpenXml.Spreadsheet.FontName fontName1 = new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" };
FontFamilyNumbering fontFamilyNumbering1 = new FontFamilyNumbering() { Val = 2 };
FontScheme fontScheme1 = new FontScheme() { Val = FontSchemeValues.Minor };
font1.Append(fontSize1);
font1.Append(color1);
font1.Append(fontName1);
font1.Append(fontFamilyNumbering1);
font1.Append(fontScheme1);
fonts1.Append(font1);
Fills fills1 = new Fills() { Count = (UInt32)(colorCode.Count + 2) };
// FillId = 0
Fill fill1 = new Fill();
PatternFill patternFill1 = new PatternFill() { PatternType = PatternValues.None };
fill1.Append(patternFill1);
fills1.Append(fill1);
// FillId = 1
Fill fill2 = new Fill();
PatternFill patternFill2 = new PatternFill() { PatternType = PatternValues.Gray125 };
fill2.Append(patternFill2);
fills1.Append(fill2);
foreach (string color in colorCode)
{
Fill fill5 = new Fill();
PatternFill patternFill5 = new PatternFill() { PatternType = PatternValues.Solid };
ForegroundColor foregroundColor3 = new ForegroundColor() { Rgb = color.Replace("#", "") };
BackgroundColor backgroundColor3 = new BackgroundColor() { Indexed = (UInt32Value)64U };
patternFill5.Append(foregroundColor3);
patternFill5.Append(backgroundColor3);
fill5.Append(patternFill5);
fills1.Append(fill5);
}
Borders borders1 = new Borders() { Count = (UInt32Value)1U };
DocumentFormat.OpenXml.Spreadsheet.Border border1 = new DocumentFormat.OpenXml.Spreadsheet.Border();
DocumentFormat.OpenXml.Spreadsheet.LeftBorder leftBorder1 = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder();
DocumentFormat.OpenXml.Spreadsheet.RightBorder rightBorder1 = new DocumentFormat.OpenXml.Spreadsheet.RightBorder();
DocumentFormat.OpenXml.Spreadsheet.TopBorder topBorder1 = new DocumentFormat.OpenXml.Spreadsheet.TopBorder();
DocumentFormat.OpenXml.Spreadsheet.BottomBorder bottomBorder1 = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder();
DocumentFormat.OpenXml.Spreadsheet.DiagonalBorder diagonalBorder1 = new DocumentFormat.OpenXml.Spreadsheet.DiagonalBorder();
border1.Append(leftBorder1);
border1.Append(rightBorder1);
border1.Append(topBorder1);
border1.Append(bottomBorder1);
border1.Append(diagonalBorder1);
borders1.Append(border1);
CellStyleFormats cellStyleFormats1 = new CellStyleFormats() { Count = (UInt32Value)1U };
CellFormat cellFormat1 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U };
cellStyleFormats1.Append(cellFormat1);
CellFormats cellFormats1 = new CellFormats() { Count = (UInt32)(colorCode.Count + 1) };
CellFormat cellFormat0 = new CellFormat()
{
NumberFormatId = (UInt32Value)0U,
FontId = (UInt32Value)0U,
FillId = (UInt32)0,
BorderId = (UInt32Value)0U,
FormatId = (UInt32Value)0U,
ApplyFill = true
};
cellFormats1.Append(cellFormat0);
int colorCodeIndex = 0;
foreach (string color in colorCode)
{
CellFormat cellFormat5 = new CellFormat()
{
NumberFormatId = (UInt32Value)0U,
FontId = (UInt32Value)0U,
FillId = (UInt32)(colorCodeIndex + 2),
BorderId = (UInt32Value)0U,
FormatId = (UInt32Value)0U,
ApplyFill = true
};
cellFormats1.Append(cellFormat5);
colorCodeIndex++;
}
CellStyles cellStyles1 = new CellStyles() { Count = (UInt32Value)1U };
CellStyle cellStyle1 = new CellStyle() { Name = "Normal", FormatId = (UInt32Value)0U, BuiltinId = (UInt32Value)0U };
cellStyles1.Append(cellStyle1);
DifferentialFormats differentialFormats1 = new DifferentialFormats() { Count = (UInt32Value)0U };
TableStyles tableStyles1 = new TableStyles() { Count = (UInt32Value)0U, DefaultTableStyle = "TableStyleMedium2", DefaultPivotStyle = "PivotStyleMedium9" };
StylesheetExtensionList stylesheetExtensionList1 = new StylesheetExtensionList();
StylesheetExtension stylesheetExtension1 = new StylesheetExtension() { Uri = "{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" };
stylesheetExtension1.AddNamespaceDeclaration("x14", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main");
DocumentFormat.OpenXml.Office2010.Excel.SlicerStyles slicerStyles1 = new DocumentFormat.OpenXml.Office2010.Excel.SlicerStyles() { DefaultSlicerStyle = "SlicerStyleLight1" };
stylesheetExtension1.Append(slicerStyles1);
stylesheetExtensionList1.Append(stylesheetExtension1);
stylesheet1.Append(fonts1);
stylesheet1.Append(fills1);
stylesheet1.Append(borders1);
stylesheet1.Append(cellStyleFormats1);
stylesheet1.Append(cellFormats1);
stylesheet1.Append(cellStyles1);
stylesheet1.Append(differentialFormats1);
stylesheet1.Append(tableStyles1);
stylesheet1.Append(stylesheetExtensionList1);
return stylesheet1;
}