如何将 xml schema/design 文件添加到 Excel Interop?

How to add xml schema/design file to Excel Interop?

我正在使用 excel 互操作

中的数据 table 创建一个 excel 文件
Price           Profit/Loss%

250.8982989       0.04301071

我有一个架构文件,其中包含设计的详细信息 1) 将所有 headers 设置为粗体 2)列定义(天气,字符串,百分比)

我在快速报告导出中使用了这个文件,但因此我必须使用互操作来 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();

我想以粗体显示此值,格式为 % 0.04301071

将此值设为粗体和圆角 250.8982989

所有这些信息都将存储在一个模式文件中,我想加载该文件

否则我想根据数据中的列数据类型加载该单元格table

我试过了:-

clmnrange.NumberFormat = (Object)table.Columns[k - 1].DataType;

但它引发了异常

问候 EP

中所述:

The NumberFormat property takes a string that uses Excel's formatting syntax. For example formatting as a percentage (up to) 8 decimal places is "0.########%"

Here 是其他人提供的示例,展示了如何实现您描述的编号格式类型:

WorkbookStylesPart sp = workbookPart.AddNewPart<WorkbookStylesPart>();

创建样式表:

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);