NPOI 生成Excel

Posted 6b23

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NPOI 生成Excel相关的知识,希望对你有一定的参考价值。

private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_FilePath.Text) || txt_FilePath.Text.Trim().Substring(txt_FilePath.Text.Trim().Length - 4) != ".xls")
            {
                MessageBox.Show("请选择要导出文件的路径和文件名,例如d:\file.xls!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                HSSFWorkbook book = new HSSFWorkbook();

                //表格样式
                //header
                HSSFCellStyle headercs = (HSSFCellStyle)book.CreateCellStyle();
                headercs.VerticalAlignment = VerticalAlignment.Center;
                headercs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                headercs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                headercs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                headercs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                headercs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                headercs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                headercs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                headercs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                headercs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;

                HSSFFont headerFont = (HSSFFont)book.CreateFont();
                headerFont.FontHeightInPoints = 11;
                headerFont.Color = HSSFColor.Black.Index;
                headerFont.FontName = "宋体";

                headercs.SetFont(headerFont);

                //文本 Style
                HSSFCellStyle stringcs = (HSSFCellStyle)book.CreateCellStyle();
                stringcs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
                stringcs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
                stringcs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                stringcs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                stringcs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                stringcs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                stringcs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                stringcs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                stringcs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                stringcs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;

                //数字style
                HSSFCellStyle decimalcs = (HSSFCellStyle)book.CreateCellStyle();
                decimalcs.DataFormat = book.CreateDataFormat().GetFormat("#0");
                decimalcs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
                decimalcs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
                decimalcs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                decimalcs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                decimalcs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                decimalcs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;

                //数字style
                HSSFCellStyle decimalcs1 = (HSSFCellStyle)book.CreateCellStyle();
                decimalcs1.DataFormat = book.CreateDataFormat().GetFormat("#0.00");
                decimalcs1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
                decimalcs1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
                decimalcs1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                decimalcs1.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                decimalcs1.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                decimalcs1.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                decimalcs1.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;

                //百分数style
                HSSFCellStyle percentagecs = (HSSFCellStyle)book.CreateCellStyle();
                //percentagecs.DataFormat = book.CreateDataFormat().GetFormat("##%");
                percentagecs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
                percentagecs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
                percentagecs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                percentagecs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                percentagecs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                percentagecs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                percentagecs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                percentagecs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                percentagecs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
                percentagecs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;

                HSSFSheet sheet2 = (HSSFSheet)book.CreateSheet("病区排程明细");

                //列名
                string[] headerString = { "类", "病区", "床号", "病历号", "姓名", "表单号", "检查科室", "检查内容", "排程日期", "开单日期", "状态" };

                HSSFRow row0_2 = (HSSFRow)sheet2.CreateRow(0);
                row0_2.CreateCell(0).SetCellValue("病区排程明细");
                row0_2.GetCell(0).CellStyle = headercs;

                sheet2.AddMergedRegion(new CellRangeAddress(0, 0, 0, headerString.Length - 1));

                HSSFRow row1_2 = (HSSFRow)sheet2.CreateRow(1);
                for (int i = 0; i < headerString.Length; i++)
                {
                    row1_2.CreateCell(i).SetCellValue(headerString[i]);
                    row1_2.GetCell(i).CellStyle = headercs;
                }
                for (int i = 0; i < dtInfo.Rows.Count; i++)
                {
                    HSSFRow row = (HSSFRow)sheet2.CreateRow(i + 2);
                    //类
                    row.CreateCell(0).SetCellValue(dtInfo.Rows[i].Cells[0].Value.ToString());
                    row.GetCell(0).CellStyle = stringcs;
                    //病区,
                    row.CreateCell(1).SetCellValue(dtInfo.Rows[i].Cells[2].Value.ToString());
                    row.GetCell(1).CellStyle = stringcs;
                    //床号,
                    row.CreateCell(2).SetCellValue(dtInfo.Rows[i].Cells[3].Value.ToString());
                    row.GetCell(2).CellStyle = stringcs;
                    //病历号,
                    row.CreateCell(3).SetCellValue(dtInfo.Rows[i].Cells[4].Value.ToString());
                    row.GetCell(3).CellStyle = stringcs;
                    //姓名,
                    row.CreateCell(4).SetCellValue(dtInfo.Rows[i].Cells[5].Value.ToString());
                    row.GetCell(4).CellStyle = stringcs;
                    //表单号
                    row.CreateCell(5).SetCellValue(dtInfo.Rows[i].Cells[6].Value.ToString());
                    row.GetCell(5).CellStyle = stringcs;
                    //检查科室,
                    row.CreateCell(6).SetCellValue(dtInfo.Rows[i].Cells[7].Value.ToString());
                    row.GetCell(6).CellStyle = stringcs;
                    //检查内容,
                    row.CreateCell(7).SetCellValue(dtInfo.Rows[i].Cells[8].Value.ToString());
                    row.GetCell(7).CellStyle = stringcs;
                    //排程日期,
                    row.CreateCell(8).SetCellValue(dtInfo.Rows[i].Cells[9].Value.ToString());
                    row.GetCell(8).CellStyle = stringcs;
                    //申请单号,
                    row.CreateCell(9).SetCellValue(dtInfo.Rows[i].Cells[10].Value.ToString());
                    row.GetCell(9).CellStyle = stringcs;
                    //状态,
                    row.CreateCell(10).SetCellValue(dtInfo.Rows[i].Cells[11].Value.ToString());
                    row.GetCell(10).CellStyle = stringcs;
                }
                sheet2.SetColumnWidth(0, 12 * 256);
                sheet2.SetColumnWidth(1, 12 * 256);
                sheet2.SetColumnWidth(2, 15 * 256);
                sheet2.SetColumnWidth(3, 13 * 256);
                sheet2.SetColumnWidth(4, 15 * 256);
                sheet2.SetColumnWidth(5, 20 * 256);
                sheet2.SetColumnWidth(6, 13 * 256);
                sheet2.SetColumnWidth(7, 40 * 256);
                sheet2.SetColumnWidth(8, 20 * 256);
                sheet2.SetColumnWidth(9, 20 * 256);
                sheet2.SetColumnWidth(10, 10 * 256);

                HSSFSheet sheetC = (HSSFSheet)book.CreateSheet("取消排程明细");
                string[] headerStringC = { "排程日期", "取消日期", "类", "病区", "床号", "病历号", "姓名", "检查科室", "检查内容", "备注" };

                HSSFRow row0_2C = (HSSFRow)sheetC.CreateRow(0);
                row0_2C.CreateCell(0).SetCellValue("取消排程明细");
                row0_2C.GetCell(0).CellStyle = headercs;

                sheetC.AddMergedRegion(new CellRangeAddress(0, 0, 0, headerStringC.Length - 1));
                HSSFRow row1_2C = (HSSFRow)sheetC.CreateRow(1);
                for (int i = 0; i < headerStringC.Length; i++)
                {
                    row1_2C.CreateCell(i).SetCellValue(headerStringC[i]);
                    row1_2C.GetCell(i).CellStyle = headercs;
                }
                for (int i = 0; i < dtInfoC.Rows.Count; i++)
                {
                    HSSFRow row = (HSSFRow)sheetC.CreateRow(i + 2);
                    //排程日期
                    row.CreateCell(0).SetCellValue(dtInfoC.Rows[i].Cells[0].Value.ToString());
                    row.GetCell(0).CellStyle = stringcs;
                    //取消日期,
                    row.CreateCell(1).SetCellValue(dtInfoC.Rows[i].Cells[1].Value.ToString());
                    row.GetCell(1).CellStyle = stringcs;
                    //类,
                    row.CreateCell(2).SetCellValue(dtInfoC.Rows[i].Cells[2].Value.ToString());
                    row.GetCell(2).CellStyle = stringcs;
                    //病区,
                    row.CreateCell(3).SetCellValue(dtInfoC.Rows[i].Cells[3].Value.ToString());
                    row.GetCell(3).CellStyle = stringcs;
                    //床号,
                    row.CreateCell(4).SetCellValue(dtInfoC.Rows[i].Cells[4].Value.ToString());
                    row.GetCell(4).CellStyle = stringcs;
                    //病历号
                    row.CreateCell(5).SetCellValue(dtInfoC.Rows[i].Cells[5].Value.ToString());
                    row.GetCell(5).CellStyle = stringcs;
                    //姓名,
                    row.CreateCell(6).SetCellValue(dtInfoC.Rows[i].Cells[6].Value.ToString());
                    row.GetCell(6).CellStyle = stringcs;
                    //检查科室,
                    row.CreateCell(7).SetCellValue(dtInfoC.Rows[i].Cells[7].Value.ToString());
                    row.GetCell(7).CellStyle = stringcs;
                    //检查内容,
                    row.CreateCell(8).SetCellValue(dtInfoC.Rows[i].Cells[8].Value.ToString());
                    row.GetCell(8).CellStyle = stringcs;
                    //备注,
                    row.CreateCell(9).SetCellValue(dtInfoC.Rows[i].Cells[9].Value.ToString());
                    row.GetCell(9).CellStyle = stringcs;
            
                }
                sheetC.SetColumnWidth(0, 20 * 256);
                sheetC.SetColumnWidth(1, 20 * 256);
                sheetC.SetColumnWidth(2, 12 * 256);
                sheetC.SetColumnWidth(3, 12 * 256);
                sheetC.SetColumnWidth(4, 15 * 256);
                sheetC.SetColumnWidth(5, 15 * 256);
                sheetC.SetColumnWidth(6, 15 * 256);
                sheetC.SetColumnWidth(7, 20 * 256);
                sheetC.SetColumnWidth(8, 40 * 256);
                sheetC.SetColumnWidth(9, 20 * 256);

                //判断是否安装EXCEL,若是已安装,则呼叫EXCEL打开
                Type officeType = Type.GetTypeFromProgID("Excel.Application");
                if (officeType == null)
                {
                    SaveFileDialog Sfd = new SaveFileDialog();
                    Sfd.Filter = "Excel文件(*.xls)|*.xls";
                    Sfd.FileName = "SCH_UCF_0090" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                    if (Sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        FileStream file3 = new FileStream(Sfd.FileName, FileMode.Create);
                        book.Write(file3);
                        file3.Close();
                        book = null;
                    }
                }
                else
                {
                    String tempPath = txt_FilePath.Text;
                    FileStream tmpxls = new FileStream(tempPath, FileMode.Create);
                    book.Write(tmpxls);
                    tmpxls.Close();
                    book = null;
                    Process.Start(tempPath);
                }
                //MessageHandling.ShowInfoMsg("CMMCMMB300", new String[] { "资料导出成功!" });
                //MessageBox.Show("资料导出成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                //MessageHandling.ShowWarnMsg("CMMCMMB300", new String[] { ex.Message });
            }
        }

以上是关于NPOI 生成Excel的主要内容,如果未能解决你的问题,请参考以下文章

使用NPOI生成Excel级联列表

NPOI 生成Excel

.Net core 利用Npoi.Mapper 生成Excel

.net 用NPOI 方式导出EXCEL ,如果自动生成相应的统计图表

WPF场景下查询数据并使用NPOI生成EXCEL表格

使用NPOI操作Excel