JAVA通过poi导出excel

maven依赖:

                <!--读取excel文件 -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.9</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.9</version>
		</dependency>

代码(多线程执行):

public class CreateExcel implements Runnable {
	// 数据
	private List<List<String>> data;
	// 表名
	private String path;
	// 列表名
	private String[] coulmns;

	public CreateExcel(List<List<String>> data, String path, String[] coulmns) {
		this.data = data;
		this.path = path;
		this.coulmns = coulmns;
	}

	private HSSFWorkbook excel;
	private HSSFSheet sheet;
	@Override
	public void run() {
		File file = new File(path);
		int begin=0;
		//判断是追加还是新建
		if (file.exists()) {
			POIFSFileSystem ps;
			try {
				ps = new POIFSFileSystem(new FileInputStream(file));
				excel = new HSSFWorkbook(ps);
				sheet = excel.getSheetAt(0);
			} catch (IOException e) {
				System.out.println("读取excel异常");
			}
			// 找到目标下一行
			HSSFRow row = sheet.getRow(0);
			if ("".equals(row) || row == null) {
				begin = 0;
			} else {
				begin = sheet.getLastRowNum();
				begin = begin + 1;
			}
		} else {
			excel = new HSSFWorkbook();
			sheet = excel.createSheet("yours data");
			sheet.setDefaultColumnWidth(30);
			// 表头
			HSSFRow row1 = sheet.createRow(0);
			row1.setHeightInPoints(25);
			// 表头样式
			HSSFCellStyle cellStyle = excel.createCellStyle();
			cellStyle.setWrapText(true);
			cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
			cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
			cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

			HSSFFont headerfont = excel.createFont();
			headerfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
			headerfont.setFontName("黑体");
			headerfont.setFontHeightInPoints((short) 12);
			cellStyle.setFont(headerfont);

			// 表头数据
			for (int i = 0; i < coulmns.length; i++) {
				HSSFCell cell = row1.createCell(i);
				cell.setCellValue(coulmns[i]);
				cell.setCellStyle(cellStyle);
			}

		}
		// 内容样式
		HSSFCellStyle contentStyle = excel.createCellStyle();
		contentStyle.setWrapText(true);// 设置自动换行
		contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式
		contentStyle.setBottomBorderColor(HSSFColor.BLACK.index);
		contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);

		// 填充内容
		HSSFRow row = null;
		System.out.println(data.get(0).size());
		for (int j = 0; j < data.get(0).size(); j++) {
			row = sheet.createRow((int) j + begin);
			HSSFCell datacell = null;
			for (int k = 0; k < data.size(); k++) {
				datacell = row.createCell(k);
				datacell.setCellValue(data.get(k).get(j));
				datacell.setCellStyle(contentStyle);
			}
		}

		// 导出数据
		try {
			System.out.println(path);
			FileOutputStream fout = new FileOutputStream(file);
			if (!file.exists()) {
				file.createNewFile();
			}
			excel.write(fout);
			System.out.println("导出成功");

		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("导出文件失败");
		}

	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值