对于java生成excel报表我还是很陌生,因为一直对poi我从没有接触过,因为工作的需要写了一个简单的demo在此做个笔记,这也是我发表的第一篇技术性的文章,希望是抛砖引玉,不足之处还望多多指教。
废话少说,我们需要导入的包
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
包可以在csdn上下载,直接搜索poi即可,也可google一下。
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.plaf.synth.Region;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class ReportTest {
public static void main(String[] args) {
ReportTest test = new ReportTest();
test.getCreateExcel();
}
public void getCreateExcel() {
// 获取当前时间,用做定义保存文件名称
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String timeNow = date.format(new Date(System.currentTimeMillis()));
System.out.println(timeNow);
// date.getDate();
// 定义存放文件的目录
String outputFile = "D:/reporttest/" + timeNow.replace(":", "")
+ ".xls";
try {
// 创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
this.getTitleCellSytle(workbook, sheet);
// 在索引1的位置创建行(最顶端的下一行)
HSSFRow row = sheet.createRow((short) 1);
// 在索引0的位置创建单元格(左上端)
HSSFCell cell;
// = row.createCell((short) 0);
// 定义单元格为字符串类型
// cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 设置cell编码解决中文高位字节截断
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
// 在单元格中输入一些内容x
int i = 0;
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("客户信息ID(customerInformationID)");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("公司名称(我的公司)companyName");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("客户名称customerName");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("客户简称nameForShort");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("公司地址companyAddress");
// 下面是填充数据到sheet中
for (int j = 0; j < 5; j++) {
i = 0;
row = sheet.createRow((short) j + 2);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("time:" + timeNow);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
}
// 写出文件
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.flush();
fOut.close();
System.out.println("文件生成......");
} catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
// TODO: handle exception
}
}
// 定义获取日期的格式的类型
public HSSFCellStyle getDateCellStyle(HSSFWorkbook workbook) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setFontName("宋体");
// font.setItalic(true);
HSSFCellStyle cellDateStyle = workbook.createCellStyle();
cellDateStyle.setFont(font);
cellDateStyle.setDataFormat(HSSFDataFormat
.getBuiltinFormat("yyyy-MM-dd HH:mm:ss"));
return cellDateStyle;
}
// 定义普通类型的列的样式
public HSSFCellStyle getGenCellStyle(HSSFWorkbook workbook) {
HSSFCellStyle cellstyle = workbook.createCellStyle();
cellstyle.setBorderBottom((short) 2);
cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
return cellstyle;
}
// 定义表头信息的样式,在索引0的位置开始
public HSSFCellStyle getTitleCellSytle(HSSFWorkbook workbook,
HSSFSheet sheet) {
HSSFRow row = sheet.createRow((short) 0);
// 合并单元格,参数分别为在0行0列的位置到0行5列的位置合并
sheet.addMergedRegion(new org.apache.poi.hssf.util.Region(0, (short) 0,
0, (short) 5));
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeight((short) 20);
font.setFontHeightInPoints((short) 20);
// font.setColor((short)HSSFColor.RED);
HSSFCellStyle titleCellStyle = workbook.createCellStyle();
// 文字居中
titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleCellStyle.setFont(font);
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(titleCellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("测试报表");
return titleCellStyle;
}
}
只 是简单的生成了一个报表样式的excel,并没有什么设计特殊的样式,如果哪位有兴趣可去参考api进行更漂亮的设计,poi的版本的不同方法也差别很 大,希望在用的时候下载最新的包,以免出错,另外poi抛出异常时更多的可能是因为方法的不正确运用,另外对于设置单元格的类型时更需注意,这里常常出 错,如日期格式等,就写这么多了,还有很多不了解的东西,希望不吝赐教.
废话少说,我们需要导入的包
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
包可以在csdn上下载,直接搜索poi即可,也可google一下。
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.plaf.synth.Region;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class ReportTest {
public static void main(String[] args) {
ReportTest test = new ReportTest();
test.getCreateExcel();
}
public void getCreateExcel() {
// 获取当前时间,用做定义保存文件名称
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String timeNow = date.format(new Date(System.currentTimeMillis()));
System.out.println(timeNow);
// date.getDate();
// 定义存放文件的目录
String outputFile = "D:/reporttest/" + timeNow.replace(":", "")
+ ".xls";
try {
// 创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
this.getTitleCellSytle(workbook, sheet);
// 在索引1的位置创建行(最顶端的下一行)
HSSFRow row = sheet.createRow((short) 1);
// 在索引0的位置创建单元格(左上端)
HSSFCell cell;
// = row.createCell((short) 0);
// 定义单元格为字符串类型
// cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 设置cell编码解决中文高位字节截断
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
// 在单元格中输入一些内容x
int i = 0;
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("客户信息ID(customerInformationID)");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("公司名称(我的公司)companyName");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("客户名称customerName");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("客户简称nameForShort");
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("公司地址companyAddress");
// 下面是填充数据到sheet中
for (int j = 0; j < 5; j++) {
i = 0;
row = sheet.createRow((short) j + 2);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("time:" + timeNow);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
cell = row.createCell((short) i++);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(this.getGenCellStyle(workbook));
cell.setCellValue("j:" + j);
}
// 写出文件
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.flush();
fOut.close();
System.out.println("文件生成......");
} catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
// TODO: handle exception
}
}
// 定义获取日期的格式的类型
public HSSFCellStyle getDateCellStyle(HSSFWorkbook workbook) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setFontName("宋体");
// font.setItalic(true);
HSSFCellStyle cellDateStyle = workbook.createCellStyle();
cellDateStyle.setFont(font);
cellDateStyle.setDataFormat(HSSFDataFormat
.getBuiltinFormat("yyyy-MM-dd HH:mm:ss"));
return cellDateStyle;
}
// 定义普通类型的列的样式
public HSSFCellStyle getGenCellStyle(HSSFWorkbook workbook) {
HSSFCellStyle cellstyle = workbook.createCellStyle();
cellstyle.setBorderBottom((short) 2);
cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
return cellstyle;
}
// 定义表头信息的样式,在索引0的位置开始
public HSSFCellStyle getTitleCellSytle(HSSFWorkbook workbook,
HSSFSheet sheet) {
HSSFRow row = sheet.createRow((short) 0);
// 合并单元格,参数分别为在0行0列的位置到0行5列的位置合并
sheet.addMergedRegion(new org.apache.poi.hssf.util.Region(0, (short) 0,
0, (short) 5));
// 设置字体
HSSFFont font = workbook.createFont();
font.setFontHeight((short) 20);
font.setFontHeightInPoints((short) 20);
// font.setColor((short)HSSFColor.RED);
HSSFCellStyle titleCellStyle = workbook.createCellStyle();
// 文字居中
titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleCellStyle.setFont(font);
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(titleCellStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("测试报表");
return titleCellStyle;
}
}
只 是简单的生成了一个报表样式的excel,并没有什么设计特殊的样式,如果哪位有兴趣可去参考api进行更漂亮的设计,poi的版本的不同方法也差别很 大,希望在用的时候下载最新的包,以免出错,另外poi抛出异常时更多的可能是因为方法的不正确运用,另外对于设置单元格的类型时更需注意,这里常常出 错,如日期格式等,就写这么多了,还有很多不了解的东西,希望不吝赐教.