您的位置:首页 > 其它

通过poi的XSSF实现生成excel文件

2017-10-26 17:23 239 查看
maven导入依赖jar包:

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


java代码:

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Demo {

public static void main(String[] args) {
// 文件内容
List<Object[]> rows = new ArrayList<Object[]>();
rows.add(new String[] { "编号", "姓名", "成绩" });
rows.add(new Object[] { 1001, "张三", 87.5F });
rows.add(new Object[] { 1002, "李四", 99.5F });
rows.add(new Object[] { 1003, "王五", null });
rows.add(new Object[] { 1004, "小六", 59F });

// 文件路径
String folderPath = "E:\\tmp";

// 文件名称
String fileName = "学生分数";

// 生成文件
new Demo().createExcelFile(rows, folderPath, fileName);
}

/**
* 根据[文件内容&文件路径&文件名称],生成一个excel文件
*/
private void createExcelFile(List<Object[]> rows, String folderPath, String fileName) {
try {
// 创建一个Workbook
XSSFWorkbook wb = new XSSFWorkbook();

// 创建一个Sheet
XSSFSheet sheet = wb.createSheet(fileName);

// 样式1:设置列宽
sheet.setColumnWidth(0, 2000); // 第一列的宽度为2000
sheet.setColumnWidth(1, 3000); // 第二列的宽度为3000

// 样式1:设置单元格背景
CellStyle titleStyle = wb.createCellStyle();
titleStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

// 样式1:设置单元格边框
titleStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框
titleStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
titleStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
titleStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框

// 样式1:设置单元格字体
Font font = wb.createFont();
// font.setColor((short) 42); // 设置字体颜色
font.setColor(HSSFColor.GREEN.index); // XSSFColor中未找到颜色和short数值的映射,使用HSSFColor来定位颜色的short值
font.setFontName("黑体"); // 设置字体
font.setFontHeightInPoints((short) 12);// 设置字体大小
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
titleStyle.setFont(font);

// 样式1:设置单元格居中
titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);

// 样式2:设置单元格居中
CellStyle contentStyle = wb.createCellStyle();
contentStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);

// 遍历输出每行
for (int i = 0; i < rows.size(); i++) {
// 创建一个row
XSSFRow row = sheet.createRow(i);

// 每一行的数据
Object[] rowData = rows.get(i);

// 遍历生成每个单元格
for (int j = 0; j < rowData.length; j++) {
// 创建一个cell
XSSFCell cell = row.createCell(j);

// 样式:设置单元格样式
if (i == 0) {
cell.setCellStyle(titleStyle);// 使用样式1
} else {
cell.setCellStyle(contentStyle);// 使用样式2
}

// 如果为空,就不做设值处理
if (null == rowData[j]) {
continue;
}

// 设值处理:假设只有四种类型的数据,如果还有其他类型,根据需要,做格式转换
// String类型数值
if (rowData[j].getClass() == String.class) {
cell.setCellValue((String) rowData[j]);
}
// double类型数值
else if (rowData[j].getClass() == double.class || rowData[j].getClass() == Double.class) {
cell.setCellValue((Double) rowData[j]);
}
// float类型数值
else if (rowData[j].getClass() == float.class || rowData[j].getClass() == Float.class) {
cell.setCellValue((Float) rowData[j]);
}
// integer类型数值
else if (rowData[j].getClass() == int.class || rowData[j].getClass() == Integer.class) {
cell.setCellValue((Integer) rowData[j]);
}
}
}

// 文件路径
String filePath = folderPath + File.separator + fileName + ".xls";// 含文件名的全路径,如果是2007及以后的版本,后缀可用.xlsx,向前兼容
File file = new File(filePath);

// 如果父目录不存在,创建父目录
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 如果已存在,删除旧文件
if (file.exists()) {
file.delete();
}

// 将excel内容写入到文件当中
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
fileOut.close();

} catch (Exception e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: