import org.apache.poi.hssf.usermodel.*; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; /** * Created by LIQJ on 9/25/2015. */ public class ExcelFileOption { public static void export() { String[] titles = new String[10]; for (int i = 0; i < titles.length; i++) { titles[i] = "列" + i; } List<String[]> list = new ArrayList(); String[] rows = new String[10]; for (int k = 0; k < 65535; k++) { for (int j = 0; j < rows.length; j++) { rows[j] = "列值" + j; } list.add(rows); } HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("Shee0"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 // HSSFCell[] cell = new HSSFCell[titles.length]; for (int i = 0; i < titles.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellValue(titles[i]); cell.setCellStyle(style); } for (int i = 0; i < list.size(); i++) { row = sheet.createRow((int) i + 1); String[] strs = list.get(i); for (int k = 0; k < strs.length; k++) { HSSFCell cell1 = row.createCell(k); cell1.setCellValue(strs[k]); } } // 第六步,将文件存到指定位置 try { FileOutputStream fout = new FileOutputStream("E:/students.xls"); wb.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { export(); } }
简单的Poi导出Excel(.xls)
最新推荐文章于 2021-02-16 11:12:30 发布