C# 如何将xml架构/设计文件添加到Excel互操作?

C# 如何将xml架构/设计文件添加到Excel互操作?,c#,xml,excel,winforms,office-interop,C#,Xml,Excel,Winforms,Office Interop,我正在使用excel interop中的数据表创建excel文件 Price Profit/Loss% 250.8982989 0.04301071 我有一个模式文件,其中包含设计的详细信息 1) 将所有标题都加粗 2) 列定义(天气、字符串、百分比) 我在快速报表导出中使用了此文件,但因此我必须使用interop for excel导出有没有办法添加该架构文件 Excel.Application excelApp = new Excel.Applicat

我正在使用excel interop中的数据表创建excel文件

Price           Profit/Loss%

250.8982989       0.04301071
我有一个模式文件,其中包含设计的详细信息 1) 将所有标题都加粗 2) 列定义(天气、字符串、百分比)

我在快速报表导出中使用了此文件,但因此我必须使用interop for excel导出有没有办法添加该架构文件

  Excel.Application excelApp = new Excel.Application();

            //Create an Excel workbook instance and open it from the predefined location
            Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);


            //Add a new worksheet to workbook with the Datatable name
            Excel.Worksheet excelWorkSheet = (Excel.Worksheet)excelWorkBook.Sheets.Add();


            for (int i = 1; i < table.Columns.Count + 1; i++)
            {
                excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
            }

            for (int j = 0; j < table.Rows.Count; j++)
            {
                for (int k = 0; k < table.Columns.Count; k++)
                {
                    excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
                }
            }

            excelWorkBook.SaveAs(@"D:\sample excel.xls");
            excelWorkBook.Close();
            excelApp.Quit();
但它提出了一个例外

问候 EP

如中所述:


NumberFormat属性接受使用Excel格式的字符串 语法。例如,格式设置为百分比(最多)8位小数 排名是“0

是其他人提供的示例,显示了如何实现所描述的numberingFormat类型:

WorkbookStylesPart sp = workbookPart.AddNewPart<WorkbookStylesPart>();
创建numberingFormat:

sp.Stylesheet.NumberingFormats = new NumberingFormats();
// #.##% is also Excel style index 1

NumberingFormat nf2decimal = new NumberingFormat();
nf2decimal.NumberFormatId = UInt32Value.FromUInt32(3453);
nf2decimal.FormatCode = StringValue.FromString("0.0%");
sp.Stylesheet.NumberingFormat.Append(nf2decimal);

我也有同样的问题,任何人都能想出一个解决方案吗?你能提供一个完整的例子(如两列两行),其中包含模式XML、数据和预期结果吗?现在的问题有点不完整(
table
甚至没有定义,…)NumberFormat属性采用使用Excel格式语法的字符串。例如,以百分比(最多)表示的小数点后8位的格式为“0。我猜您尝试分配的“DataType”属性不是Excel格式的字符串设置。我认为您需要将表格中的任何格式定义转换为Excel用于格式设置的方法。@Wedge您能举个例子说明它可能与
sp.Stylesheet = new Stylesheet();
sp.Stylesheet.NumberingFormats = new NumberingFormats();
// #.##% is also Excel style index 1

NumberingFormat nf2decimal = new NumberingFormat();
nf2decimal.NumberFormatId = UInt32Value.FromUInt32(3453);
nf2decimal.FormatCode = StringValue.FromString("0.0%");
sp.Stylesheet.NumberingFormat.Append(nf2decimal);