HSSFWorkbook导出excel

本文介绍如何利用Apache POI库在Java中批量创建包含特定样式和合并单元格的Excel文件,包括字体设置、列宽调整、单元格样式及数据填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;    

       HSSFWorkbook  excel=new HSSFWorkbook();// 创建一个Excel文件   
        HSSFSheet sheet=excel.createSheet();// 创建一个Excel的Sheet
        //设置列宽 第一个参数代表列id(从0开始),第2个参数代表宽度值  参考 :"2012-08-10"的宽度为2500
        sheet.setColumnWidth(0, 6000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 3000);
        sheet.setColumnWidth(3, 3000);
        sheet.setColumnWidth(4, 3000);
        sheet.setColumnWidth(5, 3000);
        sheet.setColumnWidth(6, 3000);
        sheet.setColumnWidth(7, 3000);
        sheet.setColumnWidth(8, 3000);
         // 设置字体   
        HSSFFont font1=excel.createFont();
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
        font1.setFontHeightInPoints((short)16);//16号字
        
        HSSFCellStyle cellstyle1=excel.createCellStyle();// Sheet样式   
        cellstyle1.setWrapText(true);//设置自动换行
        cellstyle1.setFont(font1);
        cellstyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        cellstyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        
        HSSFRow row=sheet.createRow(0); // 创建第一行   
        HSSFCell cell=row.createCell(0);// 创建第一列
        //头部第一行
        cell.setCellValue(date+" 消费数据");//第一行名称,我的是用时间+说明
        cell.setCellStyle(cellstyle1);
        
        CellRangeAddress range = new CellRangeAddress(0, 0, 0, 8);   
        sheet.addMergedRegion(range);
        
        //设置字体   
        HSSFFont font=excel.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
        
        HSSFCellStyle cellstyle=excel.createCellStyle();// Sheet样式   
        cellstyle.setWrapText(true);//设置自动换行
        cellstyle.setFont(font);//字体
        cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        //第二行
        row=sheet.createRow(1);
        cell=row.createCell(0);
        cell.setCellValue("刷卡机组");
        cell.setCellStyle(cellstyle);
        /**  
         *   合并单元格  
         *    第一个参数:第一个单元格的行数(从0开始)  
         *    第二个参数:第二个单元格的行数(从0开始)  
         *    第三个参数:第一个单元格的列数(从0开始)  
         *    第四个参数:第二个单元格的列数(从0开始)  
        */  
        range = new CellRangeAddress(1, 2, 0, 0);   
        sheet.addMergedRegion(range);
        
        range = new CellRangeAddress(1, 1, 1,2);   
        sheet.addMergedRegion(range);
        cell=row.createCell(1);
        cell.setCellValue("5:30~8:00");
        cell.setCellStyle(cellstyle);
        range = new CellRangeAddress(1, 1, 3,4);   
        sheet.addMergedRegion(range);
        cell=row.createCell(3);
        cell.setCellValue("11:30~12:30");
        cell.setCellStyle(cellstyle);
        range = new CellRangeAddress(1, 1, 5,6);   
        sheet.addMergedRegion(range);
        cell=row.createCell(5);
        cell.setCellValue("16:30~18:00");
        cell.setCellStyle(cellstyle);
        range = new CellRangeAddress(1, 1, 7,8);   
        sheet.addMergedRegion(range);
        cell=row.createCell(7);
        cell.setCellValue("21:00~22:00");
        cell.setCellStyle(cellstyle);
        //第二行
        row=sheet.createRow(2);
        cell=row.createCell(1);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(2);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(3);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(4);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(5);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(6);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(7);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(8);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        int i=2;

       //循环读取数据
        while(xfls.next()){
            i++;
            row=sheet.createRow(i);//我的从第三行开始循环数据
            for(int j=2;j<=count;j++){
                cell=row.createCell(j-2);
                cell.setCellValue(String.valueOf(xfls.getObject(j)));
            }
        }
        
        String fileName = "消费数据";//表格名称
        try {
            fileName = new String(fileName.getBytes("GB2312"), "ISO_8859_1");
        } catch (UnsupportedEncodingException e1) {
            // TODO 自动生成的 catch 块
            e1.printStackTrace();
        }
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");
        ServletOutputStream out;
        try {
            out = response.getOutputStream();
            excel.write(out);
            out.close();
            out.flush();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

 

导出的Excel效果:

HSSFWorkbook是NPOI库中用于导出Excel文件的一种方式。它是Excel的工作簿,可以包含多个工作表(HSSFSheet),而一个工作表又由多行(HSSFRow)和多个单元格(HSSFCell)组成。此外,HSSFWorkbook还提供了一些其他功能,如设置字体(HSSFFont)、日期格式(HSSFDataFormat)和单元格样式(HSSFCellStyle)。 下面是一个动态生成Excel文件的例子: ``` //创建HSSFWorkbook对象 HSSFWorkbook wb = new HSSFWorkbook(); //创建HSSFSheet对象 HSSFSheet sheet = wb.createSheet("sheet0"); //创建HSSFRow对象 HSSFRow row = sheet.createRow(0); //创建HSSFCell对象 HSSFCell cell = row.createCell(0); //设置单元格的值 cell.setCellValue("单元格中的中文"); //输出Excel文件 FileOutputStream output = new FileOutputStream("d:\\workbook.xls"); wb.write(output); output.flush(); ``` 以上代码会创建一个名为"sheet0"的工作表,并在第一行第一列的单元格中写入"单元格中的中文"。然后将工作簿保存为一个Excel文件(workbook.xls)并输出到指定路径中。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Winform中通过NPOI导出Excel的三种方式HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook示例代码.zip](https://download.youkuaiyun.com/download/qq_35406995/16604372)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [使用HSSFWorkbook导出、操作excel](https://blog.youkuaiyun.com/caicai1171523597/article/details/86643667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Java中导入、导出Excel——HSSFWorkbook 使用](https://blog.youkuaiyun.com/qq_43842093/article/details/121109593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值