繁体   English   中英

如何将xml架构/设计文件添加到Excel Interop?

[英]How to add xml schema/design file to Excel Interop?

我正在使用excel互操作中的数据表创建一个excel文件

Price           Profit/Loss%

250.8982989       0.04301071

我有一个模式文件,其中包含设计细节1)使所有标题加粗2)列定义(天气,字符串,百分比)

我在快速报告导出中使用此文件,但因此我必须使用interop for excel export有一种方法我可以添加该模式文件

  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

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

或者我想按照datatable中的columns数据类型加载该单元格

我努力了 :-

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

但它提出了一个例外

关心EP

上面评论所述

NumberFormat属性采用使用Excel格式化语法的字符串。 例如,格式化为百分比(最多)8个小数位是“0。########%”

以下是其他人提供的示例,说明如何实现您正在描述的numberingFormat类型:

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

创建样式表:

sp.Stylesheet = new Stylesheet();

创建一个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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM