Openxml出力excelファイルテープフォーマットスタイル

7020 ワード

 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();
            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(new Sheets());

            foreach (var item in dict)
            {
                var wsPart = wbPart.AddNewPart();
                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;
        }