POI动态导出数据到excel表格实例(poi3.6)

之前用过jxl动态到处数据到excel表格的,最近需使用POI3.6动态导出,方法都类似。

首先下载poi包,我这使用的是3.6版本的。

以下是测试类:

@SuppressWarnings({ "deprecation", "unchecked" })
	public static void main(String[] args) throws Exception
	{
		
		//创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		//在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("统计分析");
		
		//设置单元格宽度SetColumnWidth
		sheet.setColumnWidth(0, 15 * 256);  //15为字符数,乘以256,因为参数单位是1/256个字符宽度(0为A列,1为B列,以此类推)
		sheet.setColumnWidth(1, 15 * 256);
		sheet.setColumnWidth(4, 40 * 256);
		
		//在sheet中添加表头第0,1,3。。。行
		HSSFRow row1 = sheet.createRow((int) 0);
		HSSFRow row2 = sheet.createRow((int) 1);
		HSSFRow row3 = sheet.createRow((int) 2);
		HSSFRow row4 = sheet.createRow((int) 3);
		HSSFRow row5 = sheet.createRow((int) (row4.getRowNum()+3));
		HSSFRow row6 = sheet.createRow((int) (row5.getRowNum()+1));
		HSSFRow row7 = sheet.createRow((int) (row6.getRowNum()+1));
		
		//创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		//合并单元格
		sheet.addMergedRegion(new Region(0,(short)0,0,(short)1)); 	//四个数字分别依次为第一行,第一个单元格,第一行,第二个单元格,合并
		sheet.addMergedRegion(new Region(1,(short)0,1,(short)1));
		sheet.addMergedRegion(new Region(2,(short)0,2,(short)1)); 
		sheet.addMergedRegion(new Region(3,(short)0,3,(short)1));
		sheet.addMergedRegion(new Region(0,(short)3,0,(short)4)); 
		sheet.addMergedRegion(new Region(1,(short)3,1,(short)4));
		sheet.addMergedRegion(new Region(2,(short)3,2,(short)4)); 
		sheet.addMergedRegion(new Region(3,(short)3,3,(short)4));
		
		//创建单元格列
		HSSFCell cell1 = row1.createCell((short) 0);
		HSSFCell cell2 = row2.createCell((short) 0);
		HSSFCell cell3 = row3.createCell((short) 0);
		HSSFCell cell4 = row4.createCell((short) 0);
		HSSFCell cell5 = row5.createCell((short) 0);
		HSSFCell cell6 = row6.createCell((short) 0);
		HSSFCell cell7 = row7.createCell((short) 0);
		
		cell1.setCellValue("学校");
		cell1.setCellStyle(style);
		cell1 = row1.createCell((short) 2);
		cell1.setCellValue("人数");
		cell1.setCellStyle(style);
		cell1 = row1.createCell((short) 3);
		cell1.setCellValue("备注");
		cell1.setCellStyle(style);
		
		cell2.setCellValue("一中");
		cell2.setCellStyle(style);
		cell2 = row2.createCell((short) 2);
		cell2.setCellValue(6165);
		cell2.setCellStyle(style);
		
		cell3.setCellValue("二中");
		cell3.setCellStyle(style);
		cell3 = row3.createCell((short) 2);
		cell3.setCellValue(4153);
		cell3.setCellStyle(style);
		cell3 = row3.createCell((short) 3);
		cell3.setCellValue("备注信息");
		cell3.setCellStyle(style);
		
		cell4.setCellValue("三中");
		cell4.setCellStyle(style);
		cell4 = row4.createCell((short) 2);
		cell4.setCellValue(2534);
		cell4.setCellStyle(style);
		
		
		cell5.setCellValue("年级");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 1);
		cell5.setCellValue("各班级主任");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 2);
		cell5.setCellValue("学生数目");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 3);
		cell5.setCellValue("比例");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 4);
		cell5.setCellValue("备注");
		cell5.setCellStyle(style);
		
		cell6.setCellValue("一年级");
		cell6.setCellStyle(style);
		cell6 = row6.createCell((short) 2);
		cell6.setCellValue(213);
		cell6.setCellStyle(style);
		
		List listAAA1=new ArrayList();
		listAAA1.add("王敏");
		listAAA1.add("王丽");
		listAAA1.add("李娟");
		
		List listAAA2=new ArrayList();
		listAAA2.add(15);
		listAAA2.add(36);
		listAAA2.add(86);
		
		cell7.setCellValue("二年级");
		cell7.setCellStyle(style);
		
		for(int i=0;i<listAAA1.size();i++){
			cell7= row7.createCell((short) 1);
			cell7.setCellValue((String) listAAA1.get(i));
			cell7= row7.createCell((short) 2);
			cell7.setCellValue((Integer) listAAA2.get(i));
			row7=sheet.createRow((int)(row7.getRowNum()+1));
			
		}
		
		HSSFRow row8 = sheet.createRow((int) (row7.getRowNum()));
		HSSFCell cell8 = row8.createCell((short) 0);
		
		List listAN1=new ArrayList();
		listAN1.add("李艳");
		listAN1.add("刘芳");
		
		List listAN2=new ArrayList();
		listAN2.add(65);
		listAN2.add(55);
		
		cell8.setCellValue("三年级");
		cell8.setCellStyle(style);
		
		for(int i=0;i<listAN1.size();i++){
			cell8= row8.createCell((short) 1);
			cell8.setCellValue((String) listAN1.get(i));
			cell8= row8.createCell((short) 2);
			cell8.setCellValue((Integer) listAN2.get(i));
			row8=sheet.createRow((int)(row8.getRowNum()+1));
		}
		
		HSSFRow row9 = sheet.createRow((int) (row8.getRowNum()));
		HSSFCell cell9 = row9.createCell((short) 0);
		
		List listDUANXIN1=new ArrayList();
		listDUANXIN1.add("陈莹");
		listDUANXIN1.add("李雪");
		listDUANXIN1.add("王瑶");
		
		List listDUANXIN2=new ArrayList();
		listDUANXIN2.add(47);
		listDUANXIN2.add(94);
		listDUANXIN2.add(68);
		
		cell9.setCellValue("四年级");
		cell9.setCellStyle(style);
		
		for(int i=0;i<listDUANXIN1.size();i++){
			cell9= row9.createCell((short) 1);
			cell9.setCellValue((String) listDUANXIN1.get(i));
			cell9= row9.createCell((short) 2);
			cell9.setCellValue((Integer) listDUANXIN2.get(i));
			row9=sheet.createRow((int)(row9.getRowNum()+1));
		}
		
		HSSFRow row10 = sheet.createRow((int) (row9.getRowNum()));
		HSSFCell cell10 = row10.createCell((short) 0);
		
		List listCRBT1=new ArrayList();
		listCRBT1.add("赵梅");
		listCRBT1.add("孙彤");
		
		List listCRBT2=new ArrayList();
		listCRBT2.add(76);
		listCRBT2.add(68);
		
		cell10.setCellValue("五年级");
		cell10.setCellStyle(style);
		
		for(int i=0;i<listCRBT1.size();i++){
			cell10= row10.createCell((short) 1);
			cell10.setCellValue((String) listCRBT1.get(i));
			cell10= row10.createCell((short) 2);
			cell10.setCellValue((Integer) listCRBT2.get(i));
			row10=sheet.createRow((int)(row10.getRowNum()+1));
		}
		
		HSSFRow row11 = sheet.createRow((int) (row10.getRowNum()));
		HSSFCell cell11 = row11.createCell((short) 0);
		
		List listISCP1=new ArrayList();
		listISCP1.add("汪灵");
		listISCP1.add("李沫");
		listISCP1.add("周晗");
		
		List listISCP2=new ArrayList();
		listISCP2.add(85);
		listISCP2.add(61);
		listISCP2.add(72);
		
		cell11.setCellValue("六年级");
		cell11.setCellStyle(style);
		
		for(int i=0;i<listISCP1.size();i++){
			cell11= row11.createCell((short) 1);
			cell11.setCellValue((String) listISCP1.get(i));
			cell11= row11.createCell((short) 2);
			cell11.setCellValue((Integer) listISCP2.get(i));
			row11=sheet.createRow((int)(row11.getRowNum()+1));
		}
		
		HSSFRow row12 = sheet.createRow((int) (row11.getRowNum()));
		HSSFCell cell12 = row12.createCell((short) 0);
		
		cell12.setCellValue("七年级");
		cell12.setCellStyle(style);
		cell12 = row12.createCell((short) 2);
		cell12.setCellValue(97);
		cell12.setCellStyle(style);
		
		sheet.addMergedRegion(new Region((row12.getRowNum()+1),(short)0,(row12.getRowNum()+1),(short)1));
		
		HSSFRow row13 = sheet.createRow((int) (row12.getRowNum()+1));
		HSSFCell cell13 = row13.createCell((short) 0);
		
		cell13.setCellValue("其他");
		cell13.setCellStyle(style);
		cell13 = row13.createCell((short) 2);
		cell13.setCellValue(45);
		cell13.setCellStyle(style);
		

		//将文件生成存入硬盘
		try
		{
			FileOutputStream fout = new FileOutputStream("E:/school.xls");
			wb.write(fout);
			fout.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

导出excel表格如下:

在struts2J2EE应用中,在action使用,只贴出方法:

/**
	 * 导出excel表格数据
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	public void exportExcel(){
		student=studentService.statisticQuery(this);
		peopleList=studentService.getPeopleList();
		
		
		//创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		//在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("统计分析");
		
		//设置单元格宽度SetColumnWidth
		sheet.setColumnWidth(0, 15 * 256);  //15为字符数,乘以256,因为参数单位是1/256个字符宽度(0为A列,1为B列,以此类推)
		sheet.setColumnWidth(1, 15 * 256);
		sheet.setColumnWidth(4, 40 * 256);
		
		在sheet中添加表头第0,1,3。。。行。注意老版本poi对Excel的行数列数有限制short
		HSSFRow row1 = sheet.createRow((int) 0);
		HSSFRow row2 = sheet.createRow((int) 1);
		HSSFRow row3 = sheet.createRow((int) 2);
		HSSFRow row4 = sheet.createRow((int) 3);
		HSSFRow row14 = sheet.createRow((int) 4);
		HSSFRow row15 = sheet.createRow((int) 5);
		HSSFRow row5 = sheet.createRow((int) (row15.getRowNum()+3));
		
		
		//创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		//第一列第二列合并
		sheet.addMergedRegion(new Region(0,(short)0,0,(short)1));  //四个数字分别依次为第一行,第一个单元格,第一行,第二个单元格,合并
		sheet.addMergedRegion(new Region(1,(short)0,1,(short)1));
		sheet.addMergedRegion(new Region(2,(short)0,2,(short)1)); 
		sheet.addMergedRegion(new Region(3,(short)0,3,(short)1));
		sheet.addMergedRegion(new Region(4,(short)0,4,(short)1));
		sheet.addMergedRegion(new Region(5,(short)0,5,(short)1));
		
		//第四列第五列合并
		sheet.addMergedRegion(new Region(0,(short)3,0,(short)4)); 
		sheet.addMergedRegion(new Region(1,(short)3,1,(short)4));
		sheet.addMergedRegion(new Region(2,(short)3,2,(short)4)); 
		sheet.addMergedRegion(new Region(3,(short)3,3,(short)4));
		sheet.addMergedRegion(new Region(4,(short)3,4,(short)4));
		sheet.addMergedRegion(new Region(5,(short)3,5,(short)4));
		
		//创建单元格列
		HSSFCell cell1 = row1.createCell((short) 0);
		HSSFCell cell2 = row2.createCell((short) 0);
		HSSFCell cell3 = row3.createCell((short) 0);
		HSSFCell cell4 = row4.createCell((short) 0);
		HSSFCell cell14 = row14.createCell((short) 0);
		HSSFCell cell15 = row15.createCell((short) 0);
		HSSFCell cell5 = row5.createCell((short) 0);
		
		
		cell1.setCellValue("学校");	//单元格表头名称
		cell1.setCellStyle(style);
		cell1 = row1.createCell((short) 2);
		cell1.setCellValue("人数");	//单元格表头名称
		cell1.setCellStyle(style);
		cell1 = row1.createCell((short) 3);
		cell1.setCellValue("备注");	//单元格表头名称
		cell1.setCellStyle(style);
		
		cell2.setCellValue("一中");
		cell2.setCellStyle(style);
		cell2 = row2.createCell((short) 2);
		cell2.setCellValue(student.getSuccess());
		cell2.setCellStyle(style);
		
		cell3.setCellValue("二中");
		cell3.setCellStyle(style);
		cell3 = row3.createCell((short) 2);
		cell3.setCellValue(student.getFaile());
		cell3.setCellStyle(style);
		cell3 = row3.createCell((short) 3);
		cell3.setCellValue("备注信息");
		cell3.setCellStyle(style);
		
		cell4.setCellValue("三中");
		cell4.setCellStyle(style);
		cell4 = row4.createCell((short) 2);
		cell4.setCellValue(student.getBackoff());
		cell4.setCellStyle(style);
		
		cell14.setCellValue("总数量");
		cell14.setCellStyle(style);
		cell14 = row14.createCell((short) 2);
		cell14.setCellValue(student.getAll());
		cell14.setCellStyle(style);
		
		cell15.setCellValue("比率");
		cell15.setCellStyle(style);
		cell15 = row15.createCell((short) 2);
		cell15.setCellValue(student.getFailScale());
		cell15.setCellStyle(style);
		
		
		cell5.setCellValue("年级");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 1);
		cell5.setCellValue("各班级主任");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 2);
		cell5.setCellValue("学生数目");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 3);
		cell5.setCellValue("比例");
		cell5.setCellStyle(style);
		cell5 = row5.createCell((short) 4);
		cell5.setCellValue("备注");
		cell5.setCellStyle(style);
		
		HSSFRow row6 = sheet.createRow((int) (row5.getRowNum()+1));
		HSSFCell cell6 = row6.createCell((short) 0);
		
		cell6.setCellValue("一年级");
		cell6.setCellStyle(style);
		
		//写入动态数据
		for(Hlr hlr : peopleList.getHlrList()){
			cell6= row6.createCell((short) 1);
			cell6.setCellValue((String) hlr.getFailure_name());
			cell6.setCellStyle(style);
			cell6= row6.createCell((short) 2);
			cell6.setCellValue(Integer.valueOf(hlr.getCount()));
			cell6.setCellStyle(style);
			cell6= row6.createCell((short) 3);
			cell6.setCellValue(hlr.getScale());
			cell6.setCellStyle(style);
			row6=sheet.createRow((int)(row6.getRowNum()+1));	//创建下一行
			
		}
		
		//数据是否为空 如果为空 创建下一行
		if(peopleList.getHlrList().size()==0){
			row6=sheet.createRow((int)(row6.getRowNum()+1));
		}
		
		HSSFRow row7 = sheet.createRow((int) (row6.getRowNum()));
		HSSFCell cell7 = row7.createCell((short) 0);
		
		cell7.setCellValue("二年级");
		cell7.setCellStyle(style);
		
		for(Aaa aaa : peopleList.getAaaList()){
			cell7= row7.createCell((short) 1);
			cell7.setCellValue((String) aaa.getFailure_name());
			cell7.setCellStyle(style);
			cell7= row7.createCell((short) 2);
			cell7.setCellValue(Integer.valueOf(aaa.getCount()));
			cell7.setCellStyle(style);
			cell7= row7.createCell((short) 3);
			cell7.setCellValue(aaa.getScale());
			cell7.setCellStyle(style);
			row7=sheet.createRow((int)(row7.getRowNum()+1));
			
		}
		if(peopleList.getAaaList().size()==0){
			row7=sheet.createRow((int)(row7.getRowNum()+1));
		}
		
		HSSFRow row8 = sheet.createRow((int) (row7.getRowNum()));
		HSSFCell cell8 = row8.createCell((short) 0);
		
		cell8.setCellValue("三年级");
		cell8.setCellStyle(style);
		
		for(AnAaa anaaa : peopleList.getAnaaaList()){
			cell8= row8.createCell((short) 1);
			cell8.setCellValue((String) anaaa.getFailure_name());
			cell8.setCellStyle(style);
			cell8= row8.createCell((short) 2);
			cell8.setCellValue(Integer.valueOf(anaaa.getCount()));
			cell8.setCellStyle(style);
			cell8= row8.createCell((short) 3);
			cell8.setCellValue(anaaa.getScale());
			cell8.setCellStyle(style);
			row8=sheet.createRow((int)(row8.getRowNum()+1));
			
		}
		if(peopleList.getAnaaaList().size()==0){
			row8=sheet.createRow((int)(row8.getRowNum()+1));
		}
		
		HSSFRow row9 = sheet.createRow((int) (row8.getRowNum()));
		HSSFCell cell9 = row9.createCell((short) 0);
		
		
		cell9.setCellValue("四年级");
		cell9.setCellStyle(style);
		
		for(Duanxin duanxin : peopleList.getDuanxinList()){
			cell9= row9.createCell((short) 1);
			cell9.setCellValue((String) duanxin.getFailure_name());
			cell9.setCellStyle(style);
			cell9= row9.createCell((short) 2);
			cell9.setCellValue(Integer.valueOf(duanxin.getCount()));
			cell9.setCellStyle(style);
			cell9= row9.createCell((short) 3);
			cell9.setCellValue(duanxin.getScale());
			cell9.setCellStyle(style);
			row9=sheet.createRow((int)(row9.getRowNum()+1));
			
		}
		if(peopleList.getDuanxinList().size()==0){
			row9=sheet.createRow((int)(row9.getRowNum()+1));
		}
		
		HSSFRow row10 = sheet.createRow((int) (row9.getRowNum()));
		HSSFCell cell10 = row10.createCell((short) 0);
		
		cell10.setCellValue("五年级");
		cell10.setCellStyle(style);
		
		for(Crbt crbt : peopleList.getCrbtList()){
			cell10= row10.createCell((short) 1);
			cell10.setCellValue((String) crbt.getFailure_name());
			cell10.setCellStyle(style);
			cell10= row10.createCell((short) 2);
			cell10.setCellValue(Integer.valueOf(crbt.getCount()));
			cell10.setCellStyle(style);
			cell10= row10.createCell((short) 3);
			cell10.setCellValue((String)crbt.getScale());
			cell10.setCellStyle(style);
			row10=sheet.createRow((int)(row10.getRowNum()+1));
		}
		if(peopleList.getCrbtList().size()==0){
			row10=sheet.createRow((int)(row10.getRowNum()+1));
		}
		
		HSSFRow row11 = sheet.createRow((int) (row10.getRowNum()));
		HSSFCell cell11 = row11.createCell((short) 0);
		
		cell11.setCellValue("六年级");
		cell11.setCellStyle(style);
		
		for(Iscp iscp : peopleList.getIscpList()){
			cell11= row11.createCell((short) 1);
			cell11.setCellValue((String) iscp.getFailure_name());
			cell11.setCellStyle(style);
			cell11= row11.createCell((short) 2);
			cell11.setCellValue(Integer.valueOf(iscp.getCount()));
			cell11.setCellStyle(style);
			cell11= row11.createCell((short) 3);
			cell11.setCellValue((String)iscp.getScale());
			cell11.setCellStyle(style);
			row11=sheet.createRow((int)(row11.getRowNum()+1));
		}
		if(peopleList.getIscpList().size()==0){
			row11=sheet.createRow((int)(row11.getRowNum()+1));
		}
		
		HSSFRow row12 = sheet.createRow((int) (row11.getRowNum()));
		HSSFCell cell12 = row12.createCell((short) 0);
		
		cell12.setCellValue("七年级");
		cell12.setCellStyle(style);
		
		for(Scp scp : peopleList.getScpList()){
			cell12= row12.createCell((short) 1);
			cell12.setCellValue((String) scp.getFailure_name());
			cell12.setCellStyle(style);
			cell12= row12.createCell((short) 2);
			cell12.setCellValue(Integer.valueOf(scp.getCount()));
			cell12.setCellStyle(style);
			cell12= row12.createCell((short) 3);
			cell12.setCellValue((String)scp.getScale());
			cell12.setCellStyle(style);
			row12=sheet.createRow((int)(row12.getRowNum()+1));
		}
		if(peopleList.getScpList().size()==0){
			row12=sheet.createRow((int)(row12.getRowNum()+1));
		}
		
		//合并单元格,上一行行数加1
		sheet.addMergedRegion(new Region((row12.getRowNum()+1),(short)0,(row12.getRowNum()+1),(short)1));
		
		HSSFRow row13 = sheet.createRow((int) (row12.getRowNum()));
		HSSFCell cell13 = row13.createCell((short) 0);
		
		cell13.setCellValue("其他");
		cell13.setCellStyle(style);
		cell13 = row13.createCell((short) 2);
		cell13.setCellValue(8);
		cell13.setCellStyle(style);
		
		try
		{
			FileOutputStream fout = new FileOutputStream("E:/export.xls");
			wb.write(fout);
			fout.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}




以上部分的实现都是生成excel文件后保存到本地硬盘,在实际开发中,很多时候这样实现不可行,需要直接弹出下载文件框供用户选择下载,此时就可以这样实现:

使用ByteArrayOutputStream和ByteArrayInputStream类,思路为将HSSFWorkbook 写入ByteArrayOutputStream.然后用ByteArrayOutputStream来转换为字节流..然后再将字节流转换为ByteArrayInputStream ..这样,我们就可以把excel转换为输入流然后再输出下载。

String fileName="";(fileName为空时,会默认为action方法名称)
		
		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");  //保证不乱码
		response.setHeader("Content-Disposition","attachment;" + " filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
		OutputStream os = response.getOutputStream();


try
		{
			ByteArrayOutputStream oss =new  ByteArrayOutputStream();
			wb.write(oss);
			
			byte temp[] = oss.toByteArray();
			ByteArrayInputStream in = new ByteArrayInputStream(temp);
            int n = 0;    
            while ((n = in.read(temp)) >0) {    
                 os.write(temp, 0, n);    
             }  
			os.flush();
			os.close();
		}


web.xml文件配置:

<mime-mapping>
    <extension>xls</extension>
    <mime-type>application/vnd.ms-excel</mime-type>
  </mime-mapping>




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache POI 是一个 Java API,可用于读写 Microsoft Office 格式的文档,包括 Excel 文件。以下是使用 Apache POI数据导出Excel 的简单步骤: 1. 创建一个 Workbook 对象,表示要创建的 Excel 文件的工作簿。 ```java Workbook workbook = new XSSFWorkbook(); // 表示创建一个新的 Excel 文件 ``` 2. 创建一个 Sheet 对象,表示 Excel 文件中的一个工作表。 ```java Sheet sheet = workbook.createSheet("Sheet1"); // 创建一个名称为 "Sheet1" 的工作表 ``` 3. 创建一个 Row 对象,表示工作表中的一行。 ```java Row row = sheet.createRow(0); // 在工作表中创建第 1 行(索引从 0 开始) ``` 4. 创建 Cell 对象,表示工作表中的一个单元格,并将数据写入其中。 ```java Cell cell = row.createCell(0); // 在第 1 行中创建第 1 cell.setCellValue("Hello, world!"); // 在单元格中写入数据 ``` 5. 将数据写入 Excel 文件并保存。 ```java FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); // 创建一个文件输出流 workbook.write(fileOut); // 将工作簿中的数据写入文件 fileOut.close(); // 关闭文件输出流 ``` 完整的代码示例: ```java import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExportExcel { public static void main(String[] args) throws Exception { Workbook workbook = new XSSFWorkbook(); // 创建一个新的 Excel 文件 Sheet sheet = workbook.createSheet("Sheet1"); // 创建一个名称为 "Sheet1" 的工作表 Row row = sheet.createRow(0); // 在工作表中创建第 1 行(索引从 0 开始) Cell cell = row.createCell(0); // 在第 1 行中创建第 1 cell.setCellValue("Hello, world!"); // 在单元格中写入数据 FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); // 创建一个文件输出流 workbook.write(fileOut); // 将工作簿中的数据写入文件 fileOut.close(); // 关闭文件输出流 } } ``` 这个示例程序将创建一个名为 "workbook.xlsx" 的 Excel 文件,并在第一个工作表的第一个单元格中写入 "Hello, world!"。你可以根据需要进行修改,以适应你的数据导出需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值