티스토리 뷰

🌈 .NET MVC

NPOI 엑셀

James Wetzel 2019. 7. 11. 16:30

            var workbook = new HSSFWorkbook();
            var sheet1 = workbook.CreateSheet("Monthly Delivery Statistic");
            sheet1.SetColumnWidth(0, 7000);
            sheet1.SetColumnWidth(1, 7000);
            sheet1.SetColumnWidth(2, 7000);
            sheet1.SetColumnWidth(3, 7000);
            sheet1.SetColumnWidth(4, 7000);

            #region 헤더 정의 및 스타일 적용

            IFont boldFont = workbook.CreateFont();
            boldFont.IsBold = true;

            ICell sheet1HearderCell = null;
            ICellStyle headerStyle = workbook.CreateCellStyle();
            headerStyle.Alignment = HorizontalAlignment.Center;
            headerStyle.VerticalAlignment = VerticalAlignment.Center;
            headerStyle.WrapText = true;
            headerStyle.SetFont(boldFont);

            ICellStyle headerBorderBottomStyle = workbook.CreateCellStyle();
            headerBorderBottomStyle.Alignment = HorizontalAlignment.Center;
            headerBorderBottomStyle.VerticalAlignment = VerticalAlignment.Center;
            headerBorderBottomStyle.WrapText = true;
            headerBorderBottomStyle.SetFont(boldFont);
            headerBorderBottomStyle.BorderBottom = BorderStyle.Thick;

            IRow sheet1HeaderRow = sheet1.CreateRow(0);
            sheet1HeaderRow.Height = 500;

            sheet1HearderCell = sheet1HeaderRow.CreateCell(0);
            sheet1HearderCell.CellStyle = headerStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString("Registration date"));

            sheet1HearderCell = sheet1HeaderRow.CreateCell(1);
            sheet1HearderCell.CellStyle = headerStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString("Number of registrations"));

            sheet1HearderCell = sheet1HeaderRow.CreateCell(3);
            sheet1HearderCell.CellStyle = headerStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString("Number of Qxpress shipped"));

            IRow sheet1HeaderRow2 = sheet1.CreateRow(1);
            sheet1HeaderRow2.Height = 500;

            sheet1HearderCell = sheet1HeaderRow2.CreateCell(0);
            sheet1HearderCell.CellStyle = headerBorderBottomStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString(""));

            sheet1HearderCell = sheet1HeaderRow2.CreateCell(1);
            sheet1HearderCell.CellStyle = headerBorderBottomStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString("QFS"));

            sheet1HearderCell = sheet1HeaderRow2.CreateCell(2);
            sheet1HearderCell.CellStyle = headerBorderBottomStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString("Smart"));

            sheet1HearderCell = sheet1HeaderRow2.CreateCell(3);
            sheet1HearderCell.CellStyle = headerBorderBottomStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString("QFS"));

            sheet1HearderCell = sheet1HeaderRow2.CreateCell(4);
            sheet1HearderCell.CellStyle = headerBorderBottomStyle;
            sheet1HearderCell.SetCellValue(new HSSFRichTextString("Smart"));

            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 1, 2));
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 3, 4));
            sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, 0, 0));

            #endregion

            int sheet1RowIndex = 1;
            IRow sheet1ColumnRow = null;
            for (int i = 0; i < 2; i++)
            {
                sheet1RowIndex += 1;
                sheet1ColumnRow = sheet1.CreateRow(sheet1RowIndex);
                sheet1ColumnRow.Height = 400;

                sheet1ColumnRow.CreateCell(0).SetCellValue("0");
                sheet1ColumnRow.CreateCell(1).SetCellValue("1");
                sheet1ColumnRow.CreateCell(2).SetCellValue("2");
                sheet1ColumnRow.CreateCell(3).SetCellValue("3");
                sheet1ColumnRow.CreateCell(4).SetCellValue("4");
            }

            using (var memoryStream = new MemoryStream())
            {
                workbook.Write(memoryStream);
                byte[] bytes = memoryStream.ToArray();

                if (bytes.Length > 0)
                {
                    return File(bytes, "application/vnd.ms-excel", "Monthly Delivery Statistic.xls");
                }
            }

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함