使用NPOI的HSSFWorkbook导出数据

 public void ExportProduct(List<Model> list)
        {
            HSSFWorkbook hssfWork = new HSSFWorkbook();
            HSSFSheet sheet = (HSSFSheet)hssfWork.CreateSheet("sheet名称");
            HSSFFont font = (HSSFFont)hssfWork.CreateFont();
            font.FontHeightInPoints = 13;
            HSSFFont fontB = (HSSFFont)hssfWork.CreateFont();
            fontB.FontHeightInPoints = 16;
            fontB.Boldweight = short.MaxValue;


            #region 单元格居中样式 加粗

            HSSFCellStyle styleCenter = (HSSFCellStyle)hssfWork.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;//居中
            styleCenter.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            styleCenter.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.SetFont(fontB);
            styleCenter.WrapText = true;
            #endregion 单元格居中样式

            #region 单元格居中样式 正常

            HSSFCellStyle styleCenterNormal = (HSSFCellStyle)hssfWork.CreateCellStyle();
            styleCenterNormal.Alignment = HorizontalAlignment.Center; //居中
            styleCenterNormal.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            styleCenterNormal.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenterNormal.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenterNormal.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenterNormal.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenterNormal.SetFont(font);

            styleCenterNormal.WrapText = true;
            #endregion 单元格居中样式

            #region 表头
            sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 7));
            HSSFRow row = (HSSFRow)sheet.CreateRow(0);
            row.Height = 30 * 20;
            HSSFCell cell = (HSSFCell)row.CreateCell(0);
            cell.CellStyle = styleCenter;
            cell.SetCellValue("第一行表头");


            HSSFRow DocReportRow = (HSSFRow)sheet.CreateRow(1);
            HSSFCell[] gg = new HSSFCell[8];

            gg[0] = (HSSFCell)DocReportRow.CreateCell(0);
            gg[0].SetCellValue("序号");
            gg[0].CellStyle = styleCenter;
            sheet.SetColumnWidth(0, 100 * 20);

            gg[1] = (HSSFCell)DocReportRow.CreateCell(1);
            gg[1].SetCellValue("姓名");
            gg[1].CellStyle = styleCenter;
            sheet.SetColumnWidth(1, 356 *20);

            gg[2] = (HSSFCell)DocReportRow.CreateCell(2);
            gg[2].SetCellValue("身份证号");
            gg[2].CellStyle = styleCenter;
            sheet.SetColumnWidth(2, 356 * 30);
            
            gg[3] = (HSSFCell)DocReportRow.CreateCell(3);
            gg[3].SetCellValue("年龄");
            gg[3].CellStyle = styleCenter;
            sheet.SetColumnWidth(3, 100 * 20);

            gg[4] = (HSSFCell)DocReportRow.CreateCell(4);
            gg[4].SetCellValue("户籍地址");
            gg[4].CellStyle = styleCenter;
            sheet.SetColumnWidth(4, 356 * 40);


            gg[5] = (HSSFCell)DocReportRow.CreateCell(5);
            gg[5].SetCellValue("户口类型");
            gg[5].CellStyle = styleCenter;
            sheet.SetColumnWidth(5, 556 * 20);

            gg[6] = (HSSFCell)DocReportRow.CreateCell(6);
            gg[6].SetCellValue("性别");
            gg[6].CellStyle = styleCenter;
            sheet.SetColumnWidth(6, 556 * 20);

            gg[7] = (HSSFCell)DocReportRow.CreateCell(7);
            gg[7].SetCellValue("联系电话");
            gg[7].CellStyle = styleCenter;
            sheet.SetColumnWidth(7, 456 * 20);

            DocReportRow.HeightInPoints = 20;
            #endregion

            int rowIndex = 2;
            foreach (var item in list)
            {
                HSSFRow rowContent = (HSSFRow)sheet.CreateRow(rowIndex);
                HSSFCell[] ggcell = new HSSFCell[8];

                ggcell[0] = (HSSFCell)rowContent.CreateCell(0);
                ggcell[0].SetCellValue((rowIndex-1).ToString());
                ggcell[0].CellStyle = styleCenterNormal;

                ggcell[1] = (HSSFCell)rowContent.CreateCell(1);
                ggcell[1].SetCellValue(item.Name);
                ggcell[1].CellStyle = styleCenterNormal;

                ggcell[2] = (HSSFCell)rowContent.CreateCell(2);
                ggcell[2].SetCellValue(item.CardID);
                ggcell[2].CellStyle = styleCenterNormal;

                ggcell[3] = (HSSFCell)rowContent.CreateCell(3);
                ggcell[3].SetCellValue(item.Age);
                ggcell[3].CellStyle = styleCenterNormal;

                ggcell[4] = (HSSFCell)rowContent.CreateCell(4);
                ggcell[4].SetCellValue(item.Address);
                ggcell[4].CellStyle = styleCenterNormal;

                ggcell[5] = (HSSFCell)rowContent.CreateCell(5);
                ggcell[5].SetCellValue(item.AddressType);
                ggcell[5].CellStyle = styleCenterNormal;

                ggcell[6] = (HSSFCell)rowContent.CreateCell(6);
                ggcell[6].SetCellValue(item.Sex);
                ggcell[6].CellStyle = styleCenterNormal;

                ggcell[7] = (HSSFCell)rowContent.CreateCell(7);
                ggcell[7].SetCellValue(item.Telephone);
                ggcell[7].CellStyle = styleCenterNormal;

                rowIndex++;
            }

            //输出   
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //不保存excel在服务器直接输出到客户端进行下载
            hssfWork.Write(ms);
            string filename = string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode("导出需要的文件名称") + DateTime.Now.ToString("yyyy-MM-dd"));
            Response.AddHeader("Content-Disposition", filename);
            Response.BinaryWrite(ms.ToArray());
            hssfWork = null;
            ms.Close();
            ms.Dispose();
        }

导出如下图:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用NPOI可以很方便地在Winform应用程序中导出Excel文件。 首先,我们需要将NPOI引用添加到Winform项目中。可以通过NuGet包管理器或手动引用方式添加。 然后,我们需要创建一个工作簿对象,并添加一个工作表。可以使用HSSFWorkbook或XSSFWorkbook类来创建工作簿对象,分别对应xls和xlsx格式的Excel文件。 接下来,我们可以向工作表中添加数据。可以使用工作表中的创建行对象,然后为每行添加单元格数据。可以设置单元格的值、格式、样式等属性。 最后,我们需要将工作簿保存为Excel文件。可以使用FileStream类创建一个文件对象,并使用工作簿的Write方法将数据写入到文件中。 以下是一个简单的示例代码,将一个包含学生信息的列表导出为Excel文件: ```csharp using System; using System.Collections.Generic; using System.IO; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; // 创建工作簿和工作表 HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("学生信息"); // 添加表头 IRow headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("学号"); headerRow.CreateCell(1).SetCellValue("姓名"); headerRow.CreateCell(2).SetCellValue("年龄"); // 添加数据 List<Student> students = GetStudents(); for (int i = 0; i < students.Count; i++) { IRow dataRow = sheet.CreateRow(i + 1); dataRow.CreateCell(0).SetCellValue(students[i].Id); dataRow.CreateCell(1).SetCellValue(students[i].Name); dataRow.CreateCell(2).SetCellValue(students[i].Age); } // 保存为Excel文件 using (FileStream fileStream = new FileStream("学生信息.xls", FileMode.Create)) { workbook.Write(fileStream); } ``` 在这个示例中,我们首先创建了一个工作簿和一个工作表,并添加了表头。然后,通过获取学生信息列表来添加数据。最后,我们将工作簿保存为名为“学生信息.xls”的Excel文件。 这样,使用NPOI就可以在Winform应用程序中导出Excel文件。希望可以对你有所帮助! ### 回答2: 使用WinForm搭配NPOI导出Excel非常简单。首先,我们需要在WinForm中添加对NPOI的引用。可以通过NuGet包管理器来导入NPOI库。 导入库后,我们可以创建一个DataGridView控件来展示需要导出数据,或者直接在代码中定义一个DataTable对象来储存数据。然后,在按钮的Click事件处理程序中编写导出Excel的代码。 以下是一个简单的示例: 1. 添加一个DataGridView控件(或创建DataTable对象)并加载需要导出数据。 2. 在按钮的Click事件中添加以下代码: ```csharp using NPOI.XSSF.UserModel; // 导入XSSF命名空间 using NPOI.SS.UserModel; // 导入SS命名空间 using NPOI.HSSF.Util; // 导入HSSFUtil命名空间 using NPOI.HSSF.UserModel; // 导入HSSFUserModel命名空间 using NPOI.SS.Util; // 导入SSUtil命名空间 using NPOI.HPSF; // 导入HPSF命名空间 using NPOI.POIFS.FileSystem; // 导入POIFS命名空间 // 创建一个Excel文档对象 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建一个工作表对象 ISheet sheet = workbook.CreateSheet("Sheet1"); // 创建行和单元格 IRow row = sheet.CreateRow(0); for (int i = 0; i < dataGridView1.Columns.Count; i++) { row.CreateCell(i).SetCellValue(dataGridView1.Columns[i].HeaderText); } // 填充数据 for (int i = 0; i < dataGridView1.Rows.Count; i++) { row = sheet.CreateRow(i + 1); for (int j = 0; j < dataGridView1.Columns.Count; j++) { row.CreateCell(j).SetCellValue(dataGridView1.Rows[i].Cells[j].Value.ToString()); } } // 保存文件 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Excel文件|*.xlsx"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create)) { workbook.Write(fs); } } // 提示导出成功 MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); ``` 这是一个基本的WinForm使用NPOI导出Excel的代码示例。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值