POI操作excel文件
2013-08-09 16:19
281 查看
public void exportExcel(String title, String[] headers, List<Object[]> dataset, OutputStream out, short[] width) { // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet(title); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth((short) 15); // 生成一个样式 HSSFCellStyle style = workbook.createCellStyle(); // 设置这些样式 style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); // 生成并设置另一个样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.WHITE.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); // 产生表格标题行 HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } for(int j=0;j<dataset.size();j++){ row = sheet.createRow(j+1); for (short i = 0; i < dataset.get(j).length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style2); HSSFRichTextString richString = new HSSFRichTextString( String.valueOf( dataset.get(j)[i]).replace("null", "")); // HSSFFont font3 = workbook.createFont(); // font3.setColor(HSSFColor.BLACK.index); // richString.applyFont(font3); cell.setCellValue(richString); } } if(width!=null){ for(short i=0;i<width.length;i++){ sheet.setColumnWidth(i,width[i]); } } try { workbook.write(out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
相关文章推荐
- poi操作excel文件
- poi操作excel文件
- 用Jakarta POI操作Excel文件
- (4) 如何用Apache POI操作Excel文件-----发现了POI-3.12一个回归bug
- (8) 如何用Apache POI操作Excel文件-----POI-3.10.1 的一个黑白颜色颠倒的bug以及解决方案
- poi笔记(1)--java操作excel文件
- (5) 如何用Apache POI操作Excel文件-----发现Apache的POI的Bug后,如何给Apache的POI报Bug?
- poi操作oracle数据库导出excel文件
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI生成Excel文件(一)
- POI操作Excel文件,通过文件流判断Excel的版本
- poi操作excel文件(待完善)
- (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug
- POIjava操作excel文件
- poi关于excel文件的操作
- poi操作excel文件
- Jakarta的POI项目操作Excel文件的方便的途径
- java使用POI操作excel文件,实现批量导出,和导入
- Java Poi操作Excel文件
- jspsmart(保存文件)+poi(读取excel文件)操作excel文件
- 用Jakarta POI操作Excel文件