Java生成excel文件-本地生成excel文件-poi
package cn.itcast.poi;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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;
import org.apache.poi.ss.util.CellRangeAddress;
public class POIDemo {
public static void poiExcel(String[] propertyName, List<Worker> value)
throws FileNotFoundException, IOException {
// 创建一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 创建一个Excel的Sheet
HSSFSheet sheet = wb.createSheet("first sheet");
// 获取表需要的列数
int length = propertyName.length;
// ---------------下面设置表的第一行也就是通常的title----------------------
// 设置行-下面为第一行
HSSFRow row0 = sheet.createRow(0);
// 设置列-下面为第一行的第一列
HSSFCell cell00 = row0.createCell(0);
// 设置字体
HSSFFont headfont = wb.createFont();
headfont.setFontName("黑体");
// 字体大小
headfont.setFontHeightInPoints((short) 22);
// 加粗
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置样式
HSSFCellStyle headstyle = wb.createCellStyle();
// 使用了上面设置的字体样式
headstyle.setFont(headfont);
// 左右居中
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 上下居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
headstyle.setLocked(true);
// 自动换行
headstyle.setWrapText(true);
// 合并单元格:参数说明:1:开始行 2:结束行 3:开始列 4:结束列
// 注意,在这里使用表字段名-1来作结束列,因为我们是从0开始数的,所以要减去一个
CellRangeAddress range = new CellRangeAddress(0, 0, 0, length - 1);
// 将表的合并单元格样式设置进去
sheet.addMergedRegion(range);
// 设置表的第一行的第一列的样式
cell00.setCellStyle(headstyle);
// 设置表的第一行的第一列的value
cell00.setCellValue("POI导出EXCEL表TITLE");
// ---------------下面开始设置表的第二行,通常为字段名----------------------
HSSFRow row1 = sheet.createRow(1);
// 字段名使用的字体
HSSFFont columnHeadFont = wb.createFont();
columnHeadFont.setFontName("宋体");
// 字体大小
columnHeadFont.setFontHeightInPoints((short) 10);
// 加粗
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 列头的样式
HSSFCellStyle columnHeadStyle = wb.createCellStyle();
// 设置上面已经设置好的字体样式
columnHeadStyle.setFont(columnHeadFont);
// 左右居中
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 上下居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
// 上边框的颜色
columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index);
// 边框的大小
columnHeadStyle.setBorderTop((short) 1);
// 左边框的颜色
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
// 边框的大小
columnHeadStyle.setBorderLeft((short) 1);
// 右边框的颜色
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
// 边框的大小
columnHeadStyle.setBorderRight((short) 1);
// 设置单元格的边框为粗体
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置单元格的边框颜色
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
// 设置第二行的行高
row1.setHeight((short) 750);
// 创建在这行中的列
HSSFCell cell1 = null;
for (int i = 0; i < length; i++) {
cell1 = row1.createCell(i);
// 获取数组中的表头字段名
cell1.setCellValue(propertyName[i]);
// 给它设置风格
cell1.setCellStyle(columnHeadStyle);
}
// ---------------下面开始设置表里面的内容-----------------------------
// 设置字体
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);
// 设置其风格
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
// 左右居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 上下居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setWrapText(true);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft((short) 1);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderRight((short) 1);
// 设置单元格的边框为粗体
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置单元格的边框颜色.
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置单元格的背景颜色.
style.setFillForegroundColor(HSSFColor.WHITE.index);
HSSFRow row = null;
HSSFCell cell = null;
int valueStartRow = 2;
// 赋值
for (Worker worker : value) {
row = sheet.createRow(valueStartRow);
cell = row.createCell(0);
cell.setCellValue(worker.getId());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(worker.getName());
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(worker.getGender());
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue(worker.getAge());
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue(worker.getAddress());
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue(worker.getWage());
cell.setCellStyle(style);
valueStartRow++;
}
wb.write(new FileOutputStream("G:/excel.xls"));
}
public static void main(String[] args) throws Exception {
String[] name = { "ID", "姓名", "性别", "年龄", "地址", "工资" };
List<Worker> list = new ArrayList<Worker>();
list.add(new Worker(1, "张三", "男", 23, "北京市", "3500"));
list.add(new Worker(2, "李四", "男", 22, "上海市", "3300"));
list.add(new Worker(3, "王五", "男", 21, "深圳市", "3100"));
list.add(new Worker(4, "赵六", "男", 25, "杭州市", "3100"));
list.add(new Worker(5, "田七", "男", 26, "天津市", "3200"));
poiExcel(name, list);
}
}
项目源码:http://download.csdn.net/download/baidu\_32492845/10147840
还没有评论,来说两句吧...