依赖:
<!-- excel -->
<dependency>
<groupId>com.innoventsolutions.birt.runtime</groupId>
<artifactId>org.apache.poi_3.9.0.v201405241750</artifactId>
<version>4.8.0</version>
</dependency>
/**
* 创建新excel.
*
* @param fileDir excel的路径
* @param sheetName 要创建的表格索引
*/
public static void createExcel(String fileDir, String sheetName) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
//创建workbook
workbook = new HSSFWorkbook();
//添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
HSSFSheet sheet1 = workbook.createSheet(sheetName);
//新建文件
FileOutputStream out = null;
try {
// sheet1.setColumnWidth(9,5 * 256); 设置第九列的长度为....举个小栗子,如有需要这么设置即可
//以下为excel中样式的设置 但是毕设发现只在具体的单元格中设置才起作用 具体的在行里这么设置不起效果 详见下文
HSSFCellStyle style = workbook.createCellStyle();
style.setWrapText(true);// 设置自动换行
style.setAlignment((short) 2); // 左右居中2 居右3 默认居左
// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中1
// HSSFCellStyle style = workbook.createCellStyle();
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// HSSFFont font = workbook.createFont();
// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// font.setFontName("宋体");
// font.setFontHeightInPoints((short) 14);// 设置字体大小
//模板创建
HSSFRow row0 = workbook.getSheet(sheetName).createRow(0);
row0.setHeight((short) (2*255));
System.out.println("第一行行高:"+row0.getHeight());
row0.createCell(15).setCellValue("人");
row0.createCell(16).setCellValue("行");
row0.createCell(17).setCellValue("道");
row0.createCell(18).setCellValue("部");
row0.createCell(19).setCellValue("分");
//样式在这cell里面设置 在row2中设置不起作用
HSSFRow row2 = workbook.getSheet(sheetName).createRow(2);
row2.setHeight((short) (8*255));
HSSFCell cell24 = row2.createCell(4);
cell24.setCellStyle(style);
cell24.setCellValue("啦啦啦德玛西亚");
HSSFCell cell25 = row2.createCell(5);
cell25.setCellStyle(style);
cell25.setCellValue("");
HSSFCell cell26 = row2.createCell(6);
cell26.setCellStyle(style);
cell26.setCellValue("啦啦啦德玛西亚");
HSSFCell cell27 = row2.createCell(7);
cell27.setCellStyle(style);
cell27.setCellValue("啦啦啦德玛西亚");
HSSFCell cell28 = row2.createCell(8);
cell28.setCellStyle(style);
cell28.setCellValue("啦啦啦德玛西亚");
HSSFCell cell29 = row2.createCell(9);
cell29.setCellStyle(style);
cell29.setCellValue("啦啦啦德玛西亚");
HSSFCell cell210 = row2.createCell(10);
cell210.setCellStyle(style);
cell210.setCellValue("