openxml输出excel文件带格式样式

 private void btnTest_Click(object sender, EventArgs e)
        {
            ((Button)sender).Enabled = false;
            string dir = MyCommon.GetDeskTopTimeDir();

            string filename = AppDomain.CurrentDomain.BaseDirectory + "Export\\Export3.xml";
            //new XlsxGenerator(new ExportXmlFile(filename).GetItems()).Output(config, dir, false);
            //MyCommon.ExecuteProcess("explorer.exe", dir);

            string fname = Path.Combine(dir, "回款率报表.xlsx");
            var doc = SpreadsheetDocument.Create(fname, SpreadsheetDocumentType.Workbook);
            var wbPart = doc.AddWorkbookPart();
            var wb = new Workbook(); wbPart.Workbook = wb;



            #region 格式设置 1千分位 2百分数

            var stylesPart = doc.WorkbookPart.AddNewPart<WorkbookStylesPart>();
            stylesPart.Stylesheet = new Stylesheet();

            // blank font list
            stylesPart.Stylesheet.Fonts = new Fonts();
            stylesPart.Stylesheet.Fonts.AppendChild(new Font());

            // create fills
            stylesPart.Stylesheet.Fills = new Fills();
            stylesPart.Stylesheet.Fills.AppendChild(new Fill { PatternFill = new PatternFill { PatternType = PatternValues.None } }); // required, reserved by Excel
            stylesPart.Stylesheet.Fills.AppendChild(new Fill { PatternFill = new PatternFill { PatternType = PatternValues.Gray125 } }); // required, reserved by Excel

            // blank border list
            stylesPart.Stylesheet.Borders = new Borders();
            stylesPart.Stylesheet.Borders.AppendChild(new Border());

            // blank cell format list
            stylesPart.Stylesheet.CellStyleFormats = new CellStyleFormats();
            stylesPart.Stylesheet.CellStyleFormats.AppendChild(new CellFormat());

            // cell format list
            stylesPart.Stylesheet.CellFormats = new CellFormats();
            // empty one for index 0, seems to be required
            stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat());
            // cell format references style format 0, font 0, border 0, fill 2 and applies the fill
            stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 0, FontId = 0, BorderId = 0, FillId = 0, NumberFormatId = 4 }).AppendChild(new Alignment { Horizontal = HorizontalAlignmentValues.Right });
            stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 0, FontId = 0, BorderId = 0, FillId = 0, NumberFormatId = 10 }).AppendChild(new Alignment { Horizontal = HorizontalAlignmentValues.Right });

            #endregion

            var dict = new ExportXmlFile(Path.Combine(MyCommon.GetCurrentPath(), "Export", "Export1.xml")).GetItems();
            uint sheetNo = 1;
            var sheets = wb.AppendChild<Sheets>(new Sheets());

            foreach (var item in dict)
            {
                var wsPart = wbPart.AddNewPart<WorksheetPart>();
                var sheetData = new SheetData();
                wsPart.Worksheet = new Worksheet(sheetData);


                if (item.Key.Equals("财务"))
                {
                    string sql = item.Value;
                    DataTable dt = new SumDataTable(MyCommon.GetDt(config, sql), "小组", "应收金额,应收余额").Sum();
                    Row row = new Row();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        row.Append(new Cell() { DataType = CellValues.String, CellValue = new CellValue(dt.Columns[i].ColumnName) });
                    }
                    row.Append(new Cell() { DataType = CellValues.String, CellValue = new CellValue("回款率") });
                    sheetData.Append(row);

                    int count = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        row = new Row();
                        Cell cell = null;
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            cell = new Cell();
                            cell.DataType = GetCellValueType(dr[i]);
                            if (CellValues.Number == cell.DataType)
                            {
                                cell.StyleIndex = 1;
                            }
                            cell.CellValue = new CellValue(dr[i].ToString());
                            row.AppendChild(cell);
                        }
                        cell = new Cell();
                        cell.CellFormula = new CellFormula(string.Format("(C{0}-D{0})/C{0}", ++count));
                        cell.StyleIndex = 2;
                        row.AppendChild(cell);
                        sheetData.AppendChild(row);
                    }


                }

                else
                {
                    string sql = item.Value;
                    DataTable dt = new SumDataTable(MyCommon.GetDt(config, sql), item.Key, "应收金额,应收余额", false).Sum();
                    Row row = new Row();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        row.Append(new Cell() { DataType = CellValues.String, CellValue = new CellValue(dt.Columns[i].ColumnName) });
                    }
                    row.Append(new Cell() { DataType = CellValues.String, CellValue = new CellValue("回款率") });
                    sheetData.Append(row);

                    int count = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        row = new Row();
                        Cell cell = null;
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            cell = new Cell();
                            cell.DataType = GetCellValueType(dr[i]);
                            if (CellValues.Number == cell.DataType)
                            {
                                cell.StyleIndex = 1;
                            }
                            cell.CellValue = new CellValue(dr[i].ToString());
                            row.AppendChild(cell);
                        }
                        cell = new Cell();
                        cell.CellFormula = new CellFormula(string.Format("(B{0}-C{0})/B{0}", ++count));
                        cell.StyleIndex = 2;
                        row.AppendChild(cell);
                        sheetData.AppendChild(row);
                    }
                }
                var sheet = new Sheet() { Id = wbPart.GetIdOfPart(wsPart), SheetId = (UInt32Value)sheetNo++, Name = item.Key };
                sheets.Append(sheet);
            }

            wb.Save();
            doc.Close();

            MyCommon.ExecuteProcess("explorer.exe", dir); // fname

            this.Dispose();
            ((Button)sender).Enabled = true;
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值