C# 使用样式在现有Excel文档中添加行
我正在使用C#2008(FW3.5)、OpenXMLSDK2.0和Excel2007文档 该程序从数据库中获取用户的值并创建一个动态表,然后将这些值粘贴到Excel模板中 所有这些工作都很好,但我还需要其他东西: 我需要在模板中创建指定数量的新行,但是使用样式(边框、字体、背景色等),我不知道如何创建 有人可以帮我一个例子代码,使这C# 使用样式在现有Excel文档中添加行,c#,excel,openxml,C#,Excel,Openxml,我正在使用C#2008(FW3.5)、OpenXMLSDK2.0和Excel2007文档 该程序从数据库中获取用户的值并创建一个动态表,然后将这些值粘贴到Excel模板中 所有这些工作都很好,但我还需要其他东西: 我需要在模板中创建指定数量的新行,但是使用样式(边框、字体、背景色等),我不知道如何创建 有人可以帮我一个例子代码,使这 非常感谢,请原谅我的英语。我也遇到过同样的情况,没有发现更好的,但在excel文档中使用vb宏,它添加了新行并从.net调用它。Rastro 您可以使用以下方法来执
非常感谢,请原谅我的英语。我也遇到过同样的情况,没有发现更好的,但在excel文档中使用vb宏,它添加了新行并从.net调用它。Rastro 您可以使用以下方法来执行此操作
private UInt32Value createBorder(Stylesheet styleSheet,bool buttomBorderDouble)
{
Border border;
//set borders of header
if (buttomBorderDouble)
{
border = new Border
(
new BottomBorder { Style = BorderStyleValues.Double },
new DiagonalBorder());
}
else
{
border = new Border
(
new BottomBorder {Style = BorderStyleValues.Thin},
new DiagonalBorder());
}
styleSheet.Borders.Append(border);
UInt32Value result = styleSheet.Borders.Count;
styleSheet.Borders.Count++;
return result;
}
private UInt32Value createFont(Stylesheet styleSheet, string fontName, Nullable<double> fontSize, bool isBold, System.Drawing.Color foreColor, bool isUnderLine)
{
Font font = new Font();
if (!string.IsNullOrEmpty(fontName))
{
FontName name = new FontName()
{
Val = fontName
};
font.Append(name);
}
if (fontSize.HasValue)
{
FontSize size = new FontSize()
{
Val = fontSize.Value
};
font.Append(size);
}
if (isBold == true)
{
Bold bold = new Bold();
font.Append(bold);
}
if (isUnderLine == true)
{
Underline underline = new Underline();
font.Append(underline);
}
if (foreColor != null)
{
Color color = new Color()
{
Rgb = new HexBinaryValue()
{
Value =
System.Drawing.ColorTranslator.ToHtml(
System.Drawing.Color.FromArgb(
foreColor.A,
foreColor.R,
foreColor.G,
foreColor.B)).Replace("#", "")
}
};
font.Append(color);
}
styleSheet.Fonts.Append(font);
UInt32Value result = styleSheet.Fonts.Count;
styleSheet.Fonts.Count++;
return result;
}
private UInt32Value createCellFormat(Stylesheet styleSheet, UInt32Value fontIndex, UInt32Value fillIndex, UInt32Value numberFormatId, UInt32Value borderId)
{
CellFormat cellFormat = new CellFormat();
if (fontIndex != null)
cellFormat.FontId = fontIndex;
if (fillIndex != null)
cellFormat.FillId = fillIndex;
if (numberFormatId != null)
{
cellFormat.NumberFormatId = numberFormatId;
cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
}
if (borderId != null)
cellFormat.BorderId = borderId;
styleSheet.CellFormats.Append(cellFormat);
UInt32Value result = styleSheet.CellFormats.Count;
styleSheet.CellFormats.Count++;
return result;
}
其输出是写公司间对账汇总表,其下有粗体和双行
我希望它能帮助你
Thanks,
Mohammed Thabet Zaky
Software Developer
Cairo,Egypt
谢谢
穆罕默德·萨贝特·扎基
软件开发人员
埃及开罗
谢谢你的建议。我不可能使用宏,因为在server Office中没有安装宏。(安全问题)。 Thanks, Mohammed Thabet Zaky Software Developer Cairo,Egypt