javapoi读写数据 excel
2013-07-07 01:30
393 查看
针对2007的excel版本XSSFCELL,其他版本HSSFCELL
public HSSFWorkbook exportOrderWeek(String startDate,String endDate,String exportType){
List<Object[]> orderWeekList = dataInfoDao.orderWeekData(startDate,endDate,exportType); // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("统计结果"); //设置列宽 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 sheet.setColumnWidth(0, 2500); sheet.setColumnWidth(1, 5000); sheet.setColumnWidth(2, 5000); sheet.setColumnWidth(3, 6000); sheet.setColumnWidth(4, 2500); sheet.setColumnWidth(5, 2500); sheet.setColumnWidth(6, 5000); sheet.setColumnWidth(7, 2500); sheet.setColumnWidth(8, 5000); sheet.setColumnWidth(9, 2500); sheet.setColumnWidth(10,5000); sheet.setColumnWidth(11, 5000); sheet.setColumnWidth(12, 5000); sheet.setColumnWidth(13, 5000); sheet.setColumnWidth(14, 5000); sheet.setColumnWidth(15, 5000); sheet.setColumnWidth(16, 7000); //普通行样式 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 //第一行样式 HSSFCellStyle rowStyle = wb.createCellStyle(); rowStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 rowStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 rowStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 rowStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 rowStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 //粗体显示 HSSFFont rowFont = wb.createFont(); rowFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); rowStyle.setFont(rowFont); if(orderWeekList != null && orderWeekList.size()>0){ HSSFCell cell = row.createCell(0); cell.setCellValue("订单"); cell.setCellStyle(rowStyle); cell = row.createCell(1); cell.setCellValue("订单号"); cell.setCellStyle(rowStyle); cell = row.createCell(2); cell.setCellValue("客户昵称"); cell.setCellStyle(rowStyle); cell = row.createCell(3); cell.setCellValue("下单时间"); cell.setCellStyle(rowStyle); cell = row.createCell(4); cell.setCellValue("订单状态"); cell.setCellStyle(rowStyle); cell = row.createCell(5); cell.setCellValue("订单金额"); cell.setCellStyle(rowStyle); cell = row.createCell(6); cell.setCellValue("支付时间"); cell.setCellStyle(rowStyle); cell = row.createCell(7); cell.setCellValue("收件人"); cell.setCellStyle(rowStyle); cell = row.createCell(8); cell.setCellValue("电话"); cell.setCellStyle(rowStyle); cell = row.createCell(9); cell.setCellValue("购物状态"); cell.setCellStyle(rowStyle); cell = row.createCell(10); cell.setCellValue("收货地址(国家)"); cell.setCellStyle(rowStyle); cell = row.createCell(11); cell.setCellValue("收货地址(省)"); cell.setCellStyle(rowStyle); cell = row.createCell(12); cell.setCellValue("收货地址(市)"); cell.setCellStyle(rowStyle); cell = row.createCell(13); cell.setCellValue("收货地址(县)"); cell.setCellStyle(rowStyle); cell = row.createCell(14); cell.setCellValue("收货地址(街道)"); cell.setCellStyle(rowStyle); cell = row.createCell(15); cell.setCellValue("支付宝帐号"); cell.setCellStyle(rowStyle); cell = row.createCell(16); // cell.setCellValue("收货地址"); // cell.setCellStyle(rowStyle); // cell = row.createCell(12); // cell.setCellValue("收货地址"); // cell.setCellStyle(rowStyle); // cell = row.createCell(13); // cell.setCellValue("订单号"); // cell.setCellStyle(rowStyle); // cell = row.createCell(1); // cell.setCellValue("旧订单号"); // cell.setCellStyle(rowStyle); // cell = row.createCell(2); // cell.setCellValue("订单状态"); // cell.setCellStyle(rowStyle); // cell = row.createCell(3); // cell.setCellValue("下单时间"); // cell.setCellStyle(rowStyle); // cell = row.createCell(4); // cell.setCellValue("订单金额"); // cell.setCellStyle(rowStyle); // cell = row.createCell(5); // cell.setCellValue("单品件数"); // cell.setCellStyle(rowStyle); // cell = row.createCell(6); // cell.setCellValue("原价"); // cell.setCellStyle(rowStyle); // cell = row.createCell(7); // cell.setCellValue("现价"); // cell.setCellStyle(rowStyle); // cell = row.createCell(8); // cell.setCellValue("类别"); // cell.setCellStyle(rowStyle); // cell = row.createCell(9); // cell.setCellValue("品牌"); // cell.setCellStyle(rowStyle); // cell = row.createCell(10); // cell.setCellValue("尺寸"); // cell.setCellStyle(rowStyle); // cell = row.createCell(11); // cell.setCellValue("性别"); // cell.setCellStyle(rowStyle); // cell = row.createCell(12); // cell.setCellValue("用户昵称"); // cell.setCellStyle(rowStyle); // 第五步,写入实体数据 实际应用中这些数据从数据库得到, for (int i = 0; i < orderWeekList.size(); i++){ row = sheet.createRow(i + 1); // 第四步,创建单元格,并设置值 cell = row.createCell(0); cell.setCellValue(orderWeekList.get(i)[0] == null ? "" : orderWeekList.get(i)[0].toString()); cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(orderWeekList.get(i)[1] == null ? "" : orderWeekList.get(i)[1].toString()); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue(orderWeekList.get(i)[2] == null ? "" : orderWeekList.get(i)[2].toString()); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue(orderWeekList.get(i)[3] == null ? "" : orderWeekList.get(i)[3].toString()); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue(orderWeekList.get(i)[4] == null ? "" : orderWeekList.get(i)[4].toString()); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue(orderWeekList.get(i)[5] == null ? "" : orderWeekList.get(i)[5].toString()); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue(orderWeekList.get(i)[6] == null ? "" : orderWeekList.get(i)[6].toString()); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue(orderWeekList.get(i)[7] == null ? "" : orderWeekList.get(i)[7].toString()); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue(orderWeekList.get(i)[8] == null ? "" : orderWeekList.get(i)[8].toString()); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue(orderWeekList.get(i)[9] == null ? "" : orderWeekList.get(i)[9].toString()); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue(orderWeekList.get(i)[10] == null ? "" : orderWeekList.get(i)[10].toString()); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue(orderWeekList.get(i)[11] == null ? "" : orderWeekList.get(i)[11].toString()); cell.setCellStyle(style); cell = row.createCell(12); cell.setCellValue(orderWeekList.get(i)[12] == null ? "" : orderWeekList.get(i)[12].toString()); cell.setCellStyle(style); cell = row.createCell(13); cell.setCellValue(orderWeekList.get(i)[13] == null ? "" : orderWeekList.get(i)[13].toString()); cell.setCellStyle(style); cell = row.createCell(14); cell.setCellValue(orderWeekList.get(i)[14] == null ? "" : orderWeekList.get(i)[14].toString()); cell.setCellStyle(style); cell = row.createCell(15); cell.setCellValue(orderWeekList.get(i)[15] == null ? "" : orderWeekList.get(i)[15].toString()); cell.setCellStyle(style); // cell.setCellValue(orderWeekList.get(i)[10] == null ? "" : orderWeekList.get(i)[10].toString()); // cell.setCellStyle(style); // cell = row.createCell(11); // cell.setCellValue(orderWeekList.get(i)[11] == null ? "" : orderWeekList.get(i)[11].toString()); // cell.setCellStyle(style); // cell = row.createCell(12); // cell.setCellValue(orderWeekList.get(i)[12] == null ? "" : orderWeekList.get(i)[12].toString()); // cell.setCellStyle(style); } }
}
解析的话,如果需要上传添加到spring.xml,这和controller方法里的参数一致(@RequestParam("upload") CommonsMultipartFile files) <!-- 文件上传支持 begin --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1000000000" /> </bean> jsp: <form action="${ctx}/account/xyyy/add" method="post" enctype="multipart/form-data"> 导入卡:<input type="file" name="upload" id="exportFile"/> <input type="submit" value="确定"/> <!-- onclick="javascript:lookfile()" --> </form> public class XlsMain { public static void main( String[] args) throws IOException { XlsMain xlsMain = new XlsMain(); xlsMain.readXls(); } private void readXls() throws IOException{ InputStream is = new FileInputStream( "C:\\Users\\Mao\\Desktop\\1_1_spot_order.xlsx"); HSSFWorkbook hssfWorkbook = new HSSFWorkbook( is); // 循环工作表Sheet for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){ HSSFSheet hssfSheet = hssfWorkbook.getSheetAt( numSheet); if(hssfSheet == null){ continue; } // 循环行Row for(int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++){ HSSFRow hssfRow = hssfSheet.getRow( rowNum); if(hssfRow == null){ continue; } // 循环列Cell for(int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++){ HSSFCell hssfCell = hssfRow.getCell( cellNum); if(hssfCell == null){ continue; } System.out.print(" " + getValue( hssfCell)); } System.out.println(); } } } @SuppressWarnings("static-access") private String getValue(HSSFCell hssfCell){ if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN){ return String.valueOf( hssfCell.getBooleanCellValue()); }else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){ return String.valueOf( hssfCell.getNumericCellValue()); }else{ return String.valueOf( hssfCell.getStringCellValue()); } } } 日期类型转换 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(0) ; date = HSSFDateUtil.getJavaDate( new Double(getValue(xssfCell)));
相关文章推荐
- Java代码用POI读写Excel并修改excel格式
- JAVA POI将数据导出保存到EXCEL 的一些方式
- Java读写Excel文件中数据的简便方法
- java 调用 poi(apache的专门读写microsoft office API) 操作Excel
- Java读写Excel之POI超入门
- Java通过POI读写Excel文件——于晓昆
- 【JAVA】apache poi excel 文件读取,各种数据类型,不规则excel格式都可以读取
- java POI读写excel(项目实际需求)
- Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)
- java操作Excel之POI(5)利用POI实现使用模板批量导出数据
- java简单实现爬虫、jsoup实现网页抓取、POI实现数据导出Excel
- java poi读写excel
- JAVA通过poi对Excel数据在(jsp+ssh)环境下导入导出
- java poi Excel内图片获取存储并关联数据入库
- java poi 解析excel数据
- java 使用POI读取excel数据
- java poi 写入大量数据到excel中
- java中使用poi导出excel表格数据并且可以手动修改导出路径
- Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)