java Excel表格生成工具类

最近做数据列表导出成Excel表格,借此机会学习了一下。

首先我们需要通过hibernate链接数据库,将数据库字段映射成POJO实体,这里不多做废话。

我们需要用excel表格工具类,首先需要几个poi的jar包,poi读取excel数据所需要的jar包。其他的还需要一些commons-lang等语言包。

poi-3.9-20130828.jar

poi-ooxml-3.9-20130828.jar

poi-ooxml-schemas-3.9-20130828.jar

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)。 下面我们来看一下Apache POI 中提供的几大部分的作用:

HSSF - 提供读写Microsoft Excel XLS格式档案的功能。  
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。  
HWPF - 提供读写Microsoft Word DOC格式档案的功能。  
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。  
HDGF - 提供读Microsoft Visio格式档案的功能。  
HPBF - 提供读Microsoft Publisher格式档案的功能。  
HSMF - 提供读Microsoft Outlook格式档案的功能。

1.ExcelCreater 创建Excel表格类

package com.yuanding.common.data.excel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yuanding.common.data.excel.ExcelFile.ExcelStyle;
import com.yuanding.common.util.Constants;


public class ExcelCreater {
	
	static {
		ExcelCreater.setFilePath(Constants.Path.DOC_PATH);
	}
	
	private String fullFileName;
	private static String filePath;
	private String title;
	private String sheetTitle;
	private String[] columnTitle;
	private String[] columnKey;
	private String[] columnWidth;
	private List<Map<String, Object>> data;

	public String getFullFileName() {
		return filePath + fullFileName;
	}

	public void setFullFileName(String fullFileName) {
		this.fullFileName = fullFileName;
	}
	
	public static String getFilePath() {
		return filePath;
	}

	public static void setFilePath(String filePath) {
		File file = new File(filePath);
		if(file.exists() && file.isDirectory()){
			ExcelCreater.filePath = filePath;			
		}else{
			ExcelCreater.filePath = System.getProperty("java.io.tmpdir");
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String[] getColumnTitle() {
		return columnTitle;
	}

	public void setColumnTitle(String[] columnTitle) {
		this.columnTitle = columnTitle;
	}

	public String[] getColumnKey() {
		return columnKey;
	}

	public void setColumnKey(String[] columnKey) {
		this.columnKey = columnKey;
	}

	public String[] getColumnWidth() {
		return columnWidth;
	}

	public void setColumnWidth(String[] columnWidth) {
		this.columnWidth = columnWidth;
	}

	public List<Map<String, Object>> getData() {
		return data;
	}

	public void setData(List<Map<String, Object>> data) {
		this.data = data;
	}

	public String getSheetTitle() {
		return sheetTitle;
	}

	public void setSheetTitle(String sheetTitle) {
		this.sheetTitle = sheetTitle;
	}

	public boolean checkParam() {
		return fullFileName != null && columnTitle != null && columnKey != null;
	}

	public boolean create() {
		if (checkParam()) {
			ExcelFile excel = new ExcelFile();

			Map<String, String> sheetStyle = new HashMap<String, String>();
			sheetStyle.put(ExcelStyle.SHEET_HEAD, this.getTitle());
			sheetStyle.put(ExcelStyle.TITLE_HEIGHT, "500");
			sheetStyle.put(ExcelStyle.ROW_HEIGHT, "500");

			List<Map<String, String>> rowStyle = new ArrayList<Map<String, String>>();

			for (int i = 0, len = columnTitle.length; i < len; i++) {
				Map<String, String> styleItem1 = new HashMap<String, String>();
				styleItem1.put(ExcelStyle.CELL_TITLE, this.getColumnTitle()[i]);
				styleItem1.put(ExcelStyle.CELL_NAME, this.getColumnKey()[i]);
				styleItem1.put(ExcelSt
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成 Java 自定义 Excel 表格导出代码可以使用以下代码片段作为参考: ```java public class ExcelUtil { /** * 导出 Excel 表格 * * @param filename 文件名 * @param headers 表头 * @param data 表格数据 * @param out 输出流 * @throws IOException */ public static void exportExcel(String filename, String[] headers, List<Map<String, Object>> data, OutputStream out) throws IOException { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(filename); // 设置表头样式 XSSFCellStyle headerStyle = workbook.createCellStyle(); headerStyle.setAlignment(HorizontalAlignment.CENTER); headerStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headerStyle.setBorderTop(BorderStyle.THIN); headerStyle.setBorderBottom(BorderStyle.THIN); headerStyle.setBorderLeft(BorderStyle.THIN); headerStyle.setBorderRight(BorderStyle.THIN); // 创建表头行 XSSFRow headerRow = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { XSSFCell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); cell.setCellStyle(headerStyle); } // 设置表格数据样式 XSSFCellStyle dataStyle = workbook.createCellStyle(); dataStyle.setBorderTop(BorderStyle.THIN); dataStyle.setBorderBottom(BorderStyle.THIN); dataStyle.setBorderLeft(BorderStyle.THIN); dataStyle.setBorderRight(BorderStyle.THIN); // 填充表格数据 for (int i = 0; i < data.size(); i++) { XSSFRow dataRow = sheet.createRow(i + 1); Map<String, Object> rowData = data.get(i); for (int j = 0; j < headers.length; j++) { XSSFCell cell = dataRow.createCell(j); Object value = rowData.get(headers[j]); if (value != null) { if (value instanceof String) { cell.setCellValue((String) value); } else if (value instanceof Integer) { cell.setCellValue((Integer) value); } else if (value instanceof Double) { cell.setCellValue((Double) value); } else if (value instanceof Date) { cell.setCellValue((Date) value); XSSFCellStyle dateStyle = workbook.createCellStyle(); dateStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss")); cell.setCellStyle(dateStyle); } } cell.setCellStyle(dataStyle); } } // 自适应列宽 for (int i = 0; i < headers.length; i++) { sheet.autoSizeColumn(i); } // 输出 Excel 文件 workbook.write(out); workbook.close(); } } ``` 在这里,我们使用了 Apache POI 库来生成 Excel 表格。在 `exportExcel` 方法中,我们首先创建了一个 `XSSFWorkbook` 对象来表示 Excel 文件。然后,我们创建了一个 `XSSFSheet` 对象来表示表格,并设置了表头样式和表头行。接着,我们填充了表格数据,并根据数据类型设置了单元格样式。最后,我们自适应列宽,并将 Excel 文件输出到指定的输出流中。 要使用该工具类导出 Excel 表格,只需要调用 `exportExcel` 方法即可,如下所示: ```java List<Map<String, Object>> data = new ArrayList<>(); Map<String, Object> row1 = new LinkedHashMap<>(); row1.put("id", 1); row1.put("name", "张三"); row1.put("age", 20); row1.put("create_time", new Date()); data.add(row1); Map<String, Object> row2 = new LinkedHashMap<>(); row2.put("id", 2); row2.put("name", "李四"); row2.put("age", 22); row2.put("create_time", new Date()); data.add(row2); String[] headers = {"id", "name", "age", "create_time"}; String filename = "test.xlsx"; OutputStream out = new FileOutputStream(filename); ExcelUtil.exportExcel(filename, headers, data, out); out.close(); ``` 在这个例子中,我们首先创建了一个包含两行数据的表格数据 `data`,然后创建了一个包含表头信息的字符串数组 `headers`,并指定了导出的 Excel 文件名 `filename` 和输出流 `out`。最后,我们调用 `exportExcel` 方法导出 Excel 表格,并关闭输出流。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值