如何在生成 excel sheet 时为列值添加颜色?
How to add color for column values while generating excel sheet?
我想在 excel sheet 生成时为特定列添加颜色。
我已经生成了 excel sheet,但现在我想为列
添加颜色
//---------------------------
using (SpreadsheetDocument document = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet()
{
Id = workbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Template"
};
sheets.Append(sheet);
workbookPart.Workbook.Save();
SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
// Constructing header
Row row = new Row();
foreach (DataExchangeDefinition a in importColList)
{
defnExist = true;
row.Append(
ConstructCell(a.FieldCaption, CellValues.String));
}
if (defnExist == false)
{
row.Append(
ConstructCell("Excel Template Definition Missing", CellValues.String));
}
// Insert the header row to the Sheet Data
sheetData.AppendChild(row);
// Inserting each employee
worksheetPart.Worksheet.Save();
}
}
catch (Exception)
{
throw;
}
}
private Cell ConstructCell(string value, CellValues dataType)
{
Cell c= new Cell()
{
CellValue = new CellValue(value),
DataType = new EnumValue<CellValues>(dataType),
};
Color color2 = new Color() { Rgb = "FF006100" };
c.Append(color2);
return c;
}
这是生成 excel sheet 的代码。这个 excel sheet 只是一个模板,所以只有列名会在那里。
谁能帮我解决这个问题
以下代码将为所有列中的文本着色。 (但请注意,它不会为单个列中的文本着色。)
首先,您检查:
foreach (DataExchangeDefinition a in importColList)
{
defnExist = true;
row.Append(ConstructCell(a.FieldCaption, CellValues.String));
}
完整代码如下:
using (SpreadsheetDocument document = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Template" };
sheets.Append(sheet);
var stylesheet = new Stylesheet() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x14ac" } };
stylesheet.AddNamespaceDeclaration("mc", "http: //schemas.openxmlformats.org/markup-compatibility/2006");
stylesheet.AddNamespaceDeclaration("x14ac", "http: //schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
var fills = new Fills() { Count = 5U };
var fonts = new Fonts() { Count = 1U, KnownFonts = true };
// var cellFormats = new CellFormats() { Count = 4U };
Font font = new Font();
font.Append(new Color() { Rgb = "ff0000" });
fonts.Append(font);
//Fill fill = new Fill();
//var patternFill = new PatternFill() { PatternType = PatternValues.Solid };
//patternFill.Append(new ForegroundColor() { Rgb = "00ff00" });
//patternFill.Append(new BackgroundColor() { Indexed = 64U });
//fill.Append(patternFill);
//fills.Append(fill);
// cellFormats.AppendChild(new CellFormat() { FontId = 0U, FillId = 0U });
stylesheet.Append(fonts);
stylesheet.Append(fills);
// stylesheet.Append(cellFormats);
var stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = stylesheet;
stylePart.Stylesheet.Save();
workbookPart.Workbook.Save();
SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
// Constructing header
Row row = new Row();
foreach (DataExchangeDefinition a in importColList)
{
defnExist = true;
row.Append(
ConstructCell(a.FieldCaption, CellValues.String));
}
if (defnExist == false)
{
row.Append(
ConstructCell("Excel Template Definition Missing", CellValues.String));
}
// Insert the header row to the Sheet Data
sheetData.AppendChild(row);
// Inserting each employee
worksheetPart.Worksheet.Save();
}
}
catch (Exception)
{
throw;
}
}
private Cell ConstructCell(string value, CellValues dataType)
{
Cell c = new Cell()
{
CellValue = new CellValue(value),
DataType = new EnumValue<CellValues>(dataType)
// StyleIndex=0U,
};
return c;
}
我想在 excel sheet 生成时为特定列添加颜色。
我已经生成了 excel sheet,但现在我想为列
添加颜色 //---------------------------
using (SpreadsheetDocument document = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet()
{
Id = workbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Template"
};
sheets.Append(sheet);
workbookPart.Workbook.Save();
SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
// Constructing header
Row row = new Row();
foreach (DataExchangeDefinition a in importColList)
{
defnExist = true;
row.Append(
ConstructCell(a.FieldCaption, CellValues.String));
}
if (defnExist == false)
{
row.Append(
ConstructCell("Excel Template Definition Missing", CellValues.String));
}
// Insert the header row to the Sheet Data
sheetData.AppendChild(row);
// Inserting each employee
worksheetPart.Worksheet.Save();
}
}
catch (Exception)
{
throw;
}
}
private Cell ConstructCell(string value, CellValues dataType)
{
Cell c= new Cell()
{
CellValue = new CellValue(value),
DataType = new EnumValue<CellValues>(dataType),
};
Color color2 = new Color() { Rgb = "FF006100" };
c.Append(color2);
return c;
}
这是生成 excel sheet 的代码。这个 excel sheet 只是一个模板,所以只有列名会在那里。
谁能帮我解决这个问题
以下代码将为所有列中的文本着色。 (但请注意,它不会为单个列中的文本着色。)
首先,您检查:
foreach (DataExchangeDefinition a in importColList)
{
defnExist = true;
row.Append(ConstructCell(a.FieldCaption, CellValues.String));
}
完整代码如下:
using (SpreadsheetDocument document = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Template" };
sheets.Append(sheet);
var stylesheet = new Stylesheet() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x14ac" } };
stylesheet.AddNamespaceDeclaration("mc", "http: //schemas.openxmlformats.org/markup-compatibility/2006");
stylesheet.AddNamespaceDeclaration("x14ac", "http: //schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
var fills = new Fills() { Count = 5U };
var fonts = new Fonts() { Count = 1U, KnownFonts = true };
// var cellFormats = new CellFormats() { Count = 4U };
Font font = new Font();
font.Append(new Color() { Rgb = "ff0000" });
fonts.Append(font);
//Fill fill = new Fill();
//var patternFill = new PatternFill() { PatternType = PatternValues.Solid };
//patternFill.Append(new ForegroundColor() { Rgb = "00ff00" });
//patternFill.Append(new BackgroundColor() { Indexed = 64U });
//fill.Append(patternFill);
//fills.Append(fill);
// cellFormats.AppendChild(new CellFormat() { FontId = 0U, FillId = 0U });
stylesheet.Append(fonts);
stylesheet.Append(fills);
// stylesheet.Append(cellFormats);
var stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = stylesheet;
stylePart.Stylesheet.Save();
workbookPart.Workbook.Save();
SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
// Constructing header
Row row = new Row();
foreach (DataExchangeDefinition a in importColList)
{
defnExist = true;
row.Append(
ConstructCell(a.FieldCaption, CellValues.String));
}
if (defnExist == false)
{
row.Append(
ConstructCell("Excel Template Definition Missing", CellValues.String));
}
// Insert the header row to the Sheet Data
sheetData.AppendChild(row);
// Inserting each employee
worksheetPart.Worksheet.Save();
}
}
catch (Exception)
{
throw;
}
}
private Cell ConstructCell(string value, CellValues dataType)
{
Cell c = new Cell()
{
CellValue = new CellValue(value),
DataType = new EnumValue<CellValues>(dataType)
// StyleIndex=0U,
};
return c;
}