java 创建excel文件
简单的 java创建excel 及导出xsl
//需要三个jar包 poi-2.5.1,poi-contrib-2.5.1,poi-scratchpad-2.5.1
public static String buildExcel2() {
// 创建新的Excel 工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
// 如要新建一名为"效益指标"的工作表,其语句为
// HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFSheet sheet = wb.createSheet("sheet1");
//行
for (int index = 0; index < 8; index++) {
// 在索引0的位置创建行(第一行:sheet.createRow((short) 0);):从index判断行数
HSSFRow row = sheet.createRow((short) index);
if (index == 0) {
row.setHeightInPoints((short) 55);// 设置ex单元格的高度
} else {
row.setHeightInPoints((short) 33);// 设置ex单元格的高度
}
//列
for (int i = 0; i < 4; i++) {
// 在索引0的位置创建单元格(第一列从i中片段列数多少或者说应用格式
HSSFCell cell = row.createCell((short) i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 中文编码
HSSFCellStyle style = wb.createCellStyle();
String strValue = "";
if (index == 0) {
// 创建字体,设置其为红色、粗体:
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 16);
style.setAlignment((short) 16);
style.setBorderBottom((short) 16);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cell.setCellStyle(style);
strValue = "第一次练习ELS文档";
cell.setCellValue(strValue);
Region region = new Region(0, (short) 0, 0, (short) 3);
sheet.addMergedRegion(region);
break;
} else if (index >= 1 && index <= 4) {
switch (index) {
case 1:
if (i == 0) {
strValue = "第一教室:";
} else {
strValue = "001";
}
break;
case 2:
if (i == 0) {
strValue = "第二教室:";
} else {
strValue = "002";
}
break;
case 3:
if (i == 0) {
strValue = "第三教室:";
} else {
strValue = "003";
}
break;
default:
if (i == 0) {
strValue = "第四教室:";
} else {
strValue = "004";
}
break;
}
cell.setCellValue(strValue);
if (i == 0) {
} else {
//列的格式
Region region = new Region(index, (short) i, index,
(short) 3);
sheet.addMergedRegion(region);
}
}
}
}
String strFilePath = "C:\\";
String strFileName = "练习Els文档" + ".xls";
try {
if (!(new File(strFilePath).isDirectory())) {
new File(strFilePath).mkdirs();
}
strFilePath = strFilePath + strFileName;
File fTarget = new File(strFilePath);
// 新建一输出文件流


