使用Apache POI,实现导出Excel的功能
2014-06-12 10:18
711 查看
Apache POI是用Java编写的免费开源的Java API,它提供了对Microsoft Office格式档案读和写的功能。
Apache POI源码和库(JAR包)的官方下载地址是:http://poi.apache.org/download.html
我也提供了一个下载地址:http://download.csdn.net/detail/gaojinshan/7487253
它是最新的版本(POI-3.10-FINAL-20140208),包含源码和库(tar.gz格式)。
参考代码如下:
效果图如下:
Apache POI源码和库(JAR包)的官方下载地址是:http://poi.apache.org/download.html
我也提供了一个下载地址:http://download.csdn.net/detail/gaojinshan/7487253
它是最新的版本(POI-3.10-FINAL-20140208),包含源码和库(tar.gz格式)。
参考代码如下:
package test; import java.io.FileOutputStream; import java.io.IOException; 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 com.alibaba.fastjson.JSON; class DataInfo { private String countDate; // 统计日期 private String channelId; // 渠道号 public String getCountDate() { return countDate; } public void setCountDate(String countDate) { this.countDate = countDate; } public String getChannelId() { return channelId; } public void setChannelId(String channelId) { this.channelId = channelId; } }; public class ExportExcelTest { public static void main(String[] args) throws IOException { // 创建 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); // 创建单元格样式 HSSFCellStyle titleCellStyle = wb.createCellStyle(); // 指定单元格居中对齐,边框为细 titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); titleCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); titleCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); titleCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); titleCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); titleCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 设置填充色 titleCellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index); titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 指定当单元格内容显示不下时自动换行 titleCellStyle.setWrapText(true); // 设置单元格字体 HSSFFont titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 12); titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); titleCellStyle.setFont(titleFont); HSSFRow headerRow = sheet.createRow(0); HSSFCell headerCell = null; String[] titles = { "统计日期", "渠道号" }; for (int c = 0; c < titles.length; c++) { headerCell = headerRow.createCell(c); headerCell.setCellStyle(titleCellStyle); headerCell.setCellValue(titles[c]); sheet.setColumnWidth(c, (30 * 160)); } // ------------------------------------------------------------------ // 创建单元格样式 HSSFCellStyle cellStyle = wb.createCellStyle(); // 指定单元格居中对齐,边框为细 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 设置单元格字体 HSSFFont font = wb.createFont(); titleFont.setFontHeightInPoints((short) 11); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); cellStyle.setFont(font); String infoStr = "[{\"channelId\":\"bodao\",\"countDate\":\"2014-06-11\"}," + "{\"channelId\":\"dingzhi\",\"countDate\":\"2014-06-12\"}," + "{\"channelId\":\"ruiwei\",\"countDate\":\"2014-06-13\"}]"; List<DataInfo> list = JSON.parseArray(infoStr, DataInfo.class); for (int r = 0; r < list.size(); r++) { DataInfo item = list.get(r); HSSFRow row = sheet.createRow(r + 1); HSSFCell cell = null; int c = 0; cell = row.createCell(c++); cell.setCellStyle(cellStyle); cell.setCellValue(item.getCountDate()); cell = row.createCell(c++); cell.setCellStyle(cellStyle); cell.setCellValue(item.getChannelId()); } FileOutputStream fileOut = new FileOutputStream("test.xls"); wb.write(fileOut); fileOut.close(); System.out.println("Done"); } }
效果图如下:
相关文章推荐
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能(转)
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- 真导出Excel功能实现(使用Gembox.Spreadsheet v2.9 组件)
- Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
- Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解 3ff8
- Java 工具类 - 使用POI以及反射实现导出Excel的功能
- Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- 在Yii框架中使用PHPExcel扩展从数据库导出excel文件功能的实现
- SpringMVC/SpringBoot使用easypoi实现Excel文件导入导出功能实现
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- Java中使用Apache POI实现数据的Excel导入和导出
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- 【angularjs】pc端使用angular搭建项目,实现导出excel功能
- 在 Laravel 使用扩展包maatwebsite/excel 实现 Excel/CSV 文件导入导出功能
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能