Java中POI导出数据到Excel表中

在平常的项目中,我们总会遇到导出数据到excel表中这种情况,网上也有很多类似的方法,下面是我自己根据网上总结的一个导出数据的方法:

需要的jar包:poi-3.9.jar,poi-excelant-3.9.jar,poi-ooxml-3.9.jar,poi-ooxml-schemas-3.9.jar,poi-scratchpad-3.9.jar

jar包准备完毕,那么我们贴代码:

这是基本的工具的代码:

package com.cn.uitrs.common.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.jfinal.kit.StrKit;

/**
 * @fileName 类名:POIUtils.java
 * @author 作者:BealHe
 * @date 时间:2018年8月20日 上午11:36:55
 * @explain 类说明:导出Excel表工具
 */
public class POIUtils {

	public static boolean createFixationSheet(OutputStream os, List<Map<String, String>> list) throws IOException {
		// 创建工作薄
		HSSFWorkbook wb = new HSSFWorkbook();
		// 在工作薄上建一张工作表
		HSSFSheet sheet = wb.createSheet();
		HSSFRow row = sheet.createRow((short) 0);
		sheet.createFreezePane(0, 1);
		cteateCell(wb, row, (short) 0, "工号");
		cteateCell(wb, row, (short) 1, "姓名");
		cteateCell(wb, row, (short) 2, "部门");
		cteateCell(wb, row, (short) 3, "采样总价");
		cteateCell(wb, row, (short) 4, "分析总价");
		cteateCell(wb, row, (short) 5, "编制总价");
		cteateCell(wb, row, (short) 6, "合计");

		for (int i = 0; i < list.size();) {
			Map<String, String> map = list.get(i++);
			HSSFRow rowi = sheet.createRow((short) (i));
			String userId = StrKit.isBlank(map.get("userId")) ? "" : map.get("userId");
			cteateCell(wb, rowi, (short) 0, userId);
			String realName = StrKit.isBlank(map.get("realName")) ? "" : map.get("realName");
			cteateCell(wb, rowi, (short) 1, realName);
			String depart = StrKit.isBlank(map.get("depart")) ? "" : map.get("depart");
			cteateCell(wb, rowi, (short) 2, depart);
			String sample = StrKit.isBlank(map.get("sample")) ? "" : map.get("sample");
			cteateCell(wb, rowi, (short) 3, sample);
			String test = StrKit.isBlank(map.get("test")) ? "" : map.get("test");
			cteateCell(wb, rowi, (short) 4, test);
			String report = StrKit.isBlank(map.get("report")) ? "" : map.get("report");
			cteateCell(wb, rowi, (short) 5, report);
			String heji = StrKit.isBlank(map.get("heji")) ? "" : map.get("heji");
			cteateCell(wb, rowi, (short) 6, heji);
		}

		wb.write(os);
		os.flush();
		os.close();
		System.out.println("绩效文件生成成功!");
		return true;
	}

	private static void cteateCell(HSSFWorkbook wb, HSSFRow row, short col, String val) {
		@SuppressWarnings("deprecation")
		HSSFCell cell = row.createCell(col);
		cell.setCellValue(val);
		HSSFCellStyle cellstyle = wb.createCellStyle();
		cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
		cell.setCellStyle(cellstyle);
	}

}

基本工具是这样的,其实很简单,网上类似工具都是差不多的,这也是我自己根据网上总结的,可能没有别人的那么好。

然后是调用工具的方法:

// Excel文件名
String name = String.valueOf(new Date().getTime());

HttpServletResponse response = getResponse();
// 取得输出流
OutputStream os = response.getOutputStream();
response.reset();
// 设定输出文件头,该方法有两个参数,分别表示应答头的名字和值。
response.setHeader("Content-disposition", "attachment; filename=" + name + ".xls");
response.setContentType("application/msexcel");

POIUtils.createFixationSheet(os, result);

result里面的数据什么的,我就没有给出了,这个大家如果用这个方法的话,随便加点数据测试就可以了。由于我使用的是JFinal的框架,所以获取HttpServletResponse的方式是这样的,大家自己获取HttpServletResponse时根据自己实际情况来定。

下面是导出数据结果:

 

好的,以下是一个简单的 Java 代码示例,演示如何将图片导出Excel 使用 POI 库: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.util.IOUtils; import org.apache.poi.ss.util.ImageUtils; public class PoiExportImageToExcelExample { public static void main(String[] args) throws IOException { String imagePath = "path_to_image.png"; String excelPath = "path_to_excel.xlsx"; int rowIndex = 0; int columnIndex = 0; // 创建 Excel 工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 加载图片文件 byte[] imageData = IOUtils.toByteArray(new FileInputStream(imagePath)); // 插入图片到单元格 Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, columnIndex, rowIndex, columnIndex + 1, rowIndex + 1); Picture picture = drawing.createPicture(anchor, workbook.addPicture(imageData, Workbook.PICTURE_TYPE_PNG)); // 输出到 Excel 文件 FileOutputStream fileOut = new FileOutputStream(excelPath); workbook.write(fileOut); fileOut.close(); workbook.close(); System.out.println("图片已经成功导出Excel 文件!"); } } ``` 这个示例假设图片已经存在于本地文件系统,你需要将 `path_to_image.png` 替换为你的图片路径。它还假设你想将图片插入到单元格的第一行第一列,如果你想插入到其他位置,你需要修改 `rowIndex` 和 `columnIndex` 变量的值。 这个示例使用 POI 的 `Workbook` 和 `Sheet` 类创建了一个 Excel 文件,然后使用 `Drawing` 和 `ClientAnchor` 类将图片插入到单元格。最后,使用 `FileOutputStream` 将工作簿写入到本地文件系统
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值