如何在使用ExcelHelper类导出多个表格时设置单元格的宽度和背景颜色

我可以在Excel文件中创build多个工作表但是我无法通过给列指定适当的宽度来格式化单元格

我需要显示生成的Excel如:

column1 column2 

dataaaaaaaaaaaaaaaaiaaaaaaa 4数据数据ggghjkkllil

aaaaaddfdfffgggggggggggggg 5

column1和column2应该居中alignment,而column1和column2数据应该是以蓝色作为背景的包装文本

这需要列给予适当的宽度和文本应该是包装文本。不幸的是我无法做适当的风格。而且我想要应用背景颜色的单元格。对于我使用的背景颜色

 sb.AppendFormat(@" <Interior ss:Color=""#2E2EFE""/>{0}", Environment.NewLine); 

但它的工作。 在这里,我正在给我的代码。

  StringBuilder sb = new StringBuilder(818); sb.AppendFormat(@"<?xml version=""1.0""?>{0}", Environment.NewLine); sb.AppendFormat(@"<?mso-application progid=""Excel.Sheet""?>{0}", Environment.NewLine); sb.AppendFormat(@"<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine); sb.AppendFormat(@" xmlns:o=""urn:schemas-microsoft-com:office:office""{0}", Environment.NewLine); sb.AppendFormat(@" xmlns:x=""urn:schemas-microsoft-com:office:excel""{0}", Environment.NewLine); sb.AppendFormat(@" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine); sb.AppendFormat(@" xmlns:html=""http://www.w3.org/TR/REC-html40"">{0}", Environment.NewLine); sb.AppendFormat(@" <Styles>{0}", Environment.NewLine); sb.AppendFormat(@" <Style ss:ID=""Default"" ss:Name=""Normal"">{0}", Environment.NewLine); sb.AppendFormat(@" <Alignment ss:Vertical=""Bottom""/>{0}", Environment.NewLine); sb.AppendFormat(@" <Borders/>{0}", Environment.NewLine); sb.AppendFormat(@" <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""/>{0}", Environment.NewLine); sb.AppendFormat(@" <Interior/>{0}", Environment.NewLine); sb.AppendFormat(@" <NumberFormat/>{0}", Environment.NewLine); sb.AppendFormat(@" <Protection/>{0}", Environment.NewLine); sb.AppendFormat(@" </Style>{0}", Environment.NewLine); sb.AppendFormat(@" <Style ss:ID=""s62"">{0}", Environment.NewLine); sb.AppendFormat(@" <Alignment ss:Vertical=""Bottom""/>{0}", Environment.NewLine); sb.AppendFormat(@" <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""14"" ss:Color=""#000000""{0} ", Environment.NewLine); sb.AppendFormat(@" ss:Bold=""1""/>{0}", Environment.NewLine); sb.AppendFormat(@" </Style>{0}", Environment.NewLine); sb.AppendFormat(@" <Style ss:ID=""s65"">{0}", Environment.NewLine); sb.AppendFormat(@" <ss:Borders>{0}", Environment.NewLine); sb.AppendFormat(@" <ss:Border ss:Position=""Bottom"" ss:LineStyle=""Continuous"" ss:Weight=""1"" />{0}",Environment.NewLine); sb.AppendFormat(@" <ss:Border ss:Position=""Top"" ss:LineStyle=""Continuous"" ss:Weight=""1"" />{0}", Environment.NewLine); sb.AppendFormat(@" <ss:Border ss:Position=""Left"" ss:LineStyle=""Continuous"" ss:Weight=""1"" />{0}",Environment.NewLine); sb.AppendFormat(@" <ss:Border ss:Position=""Right"" ss:LineStyle=""Continuous"" ss:Weight=""1"" />{0}", Environment.NewLine); sb.AppendFormat(@" </ss:Borders>{0}", Environment.NewLine); sb.AppendFormat(@" <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""12"" ss:Color=""#2E2EFE""{0} ", Environment.NewLine); sb.AppendFormat(@" ss:Bold=""1""/>{0}", Environment.NewLine); sb.AppendFormat(@" </Style>{0}", Environment.NewLine); sb.AppendFormat(@" <Style ss:ID=""s63"">{0}", Environment.NewLine); sb.AppendFormat(@" <NumberFormat ss:Format=""Short Date""/>{0}", Environment.NewLine); sb.AppendFormat(@" </Style>{0}", Environment.NewLine); sb.AppendFormat(@" </Styles>{0}", Environment.NewLine); sb.Append(@"{0}\r\n</Workbook>"); return sb.ToString(); 

你能帮我这么做吗

我想它太晚了,也许你已经有这个解决scheme。 但我仍然给予解决scheme,对我来说工作得很好。

为了添加下面的代码使用Background: –

  sb.AppendFormat(@" <Style ss:ID=""backgroundYellow"">{0}", Environment.NewLine); sb.AppendFormat(@" <Interior ss:Color= ""#008000"" ss:Pattern=""Solid"" />{0}", Environment.NewLine); sb.AppendFormat(@" </Style>{0}", Environment.NewLine); 

您错过了标签ss:Pattern =“”Solid“”