1.引入jar包依赖
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency>
2.编写代码测试
1 package testweb; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 import java.util.LinkedList; 9 import java.util.List; 10 11 import org.apache.poi.hssf.usermodel.HSSFCell; 12 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 13 import org.apache.poi.hssf.usermodel.HSSFFont; 14 import org.apache.poi.hssf.usermodel.HSSFRichTextString; 15 import org.apache.poi.hssf.usermodel.HSSFRow; 16 import org.apache.poi.hssf.usermodel.HSSFSheet; 17 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 18 import org.apache.poi.hssf.util.HSSFColor; 19 20 21 public class TestExcel { 22 23 public static void main(String[] args) { 24 List<Object> l=new LinkedList<>(); 25 l.add("zs"); 26 l.add("ls"); 27 l.add("we"); 28 l.add("mz"); 29 String[] headers=new String[]{"tou1","tou2","tou3","tou4"}; 30 try { 31 OutputStream o= new FileOutputStream(new File("C:/Users/yanan/Desktop/yanantest.xls")); 32 exportDataExcel("nihao",headers,l,o); 33 } catch (FileNotFoundException e) { 34 e.printStackTrace(); 35 } 36 } 37 38 protected static void exportDataExcel(String title,String[] headers,List<Object> mapList,OutputStream out){ 39 //声明一个工作簿 40 HSSFWorkbook workbook = new HSSFWorkbook(); 41 //生成一个表格 42 HSSFSheet sheet = workbook.createSheet(title); 43 //设置表格默认列宽度字符 44 sheet.setDefaultColumnWidth(20); 45 //生成一个样式,用来设置标题样式 46 HSSFCellStyle style = workbook.createCellStyle(); 47 //设置这些样式 48 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); 49 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 50 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 51 style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 52 style.setBorderRight(HSSFCellStyle.BORDER_THIN); 53 style.setBorderTop(HSSFCellStyle.BORDER_THIN); 54 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 55 //生成一个字体 56 HSSFFont font = workbook.createFont(); 57 font.setColor(HSSFColor.VIOLET.index); 58 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 59 //把字体应用到当前的样式 60 style.setFont(font); 61 // 生成并设置另一个样式,用于设置内容样式 62 HSSFCellStyle style2 = workbook.createCellStyle(); 63 style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); 64 style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 65 style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); 66 style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); 67 style2.setBorderRight(HSSFCellStyle.BORDER_THIN); 68 style2.setBorderTop(HSSFCellStyle.BORDER_THIN); 69 style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); 70 style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 71 // 生成另一个字体 72 HSSFFont font2 = workbook.createFont(); 73 font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 74 // 把字体应用到当前的样式 75 style2.setFont(font2); 76 //产生表格标题行 77 HSSFRow row = sheet.createRow(0); 78 for (int i = 0; i < headers.length; i++) { 79 HSSFCell cell = row.createCell(i); 80 cell.setCellStyle(style); 81 HSSFRichTextString text = new HSSFRichTextString(headers[i]); 82 cell.setCellValue(text); 83 } 84 for (int i = 0; i < mapList.size(); i++) { 85 row = sheet.createRow(i + 1); 86 int j = 0; 87 HSSFCell cell = row.createCell(j++); 88 cell.setCellValue("循环获得值1"); 89 cell.setCellStyle(style2); 90 row.createCell(j++).setCellValue("循环获得值2"); 91 row.createCell(j++).setCellValue("循环获得值3"); 92 row.createCell(j++).setCellValue("循环获得值4"); 93 } 94 try { 95 workbook.write(out); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 }
package testweb; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; public class TestExcel { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("C:/Users/yanan/Desktop/yanan.xls"); List<Map<String, Object>> yanantest = duquexcel(file); for (Map<String, Object> item : yanantest) { System.out.println(item.get("id") + "," + item.get("name") + "," + item.get("gendar")+","+item.get("time")); } } catch (FileNotFoundException e) { e.printStackTrace(); } } public static List<Map<String, Object>> duquexcel(InputStream fis) { List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>(); HSSFWorkbook wb; try { wb = new HSSFWorkbook(new POIFSFileSystem(fis)); Sheet sheet = wb.getSheetAt(0); // 日期格式化 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/dd"); // 数字格式化 DecimalFormat df = new DecimalFormat("##"); // 循环xls中的每个表格 Row firstRow = sheet.getRow(0); for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); Map<String, Object> rowMap = new HashMap<String, Object>(); for (int k = 0; k < row.getLastCellNum(); k++) { Cell cell = row.getCell(k); if (null == cell) { continue; } switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if(HSSFDateUtil.isCellDateFormatted(cell)){ rowMap.put(firstRow.getCell(k).getStringCellValue(),sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())) ); break; } double value_d = cell.getNumericCellValue(); long value_l = (long) cell.getNumericCellValue();// cell.getCellStyle()获取样式 if (value_d == value_l) rowMap.put(firstRow.getCell(k).getStringCellValue(), String.valueOf(value_l)); else rowMap.put(firstRow.getCell(k).getStringCellValue(), String.valueOf(value_d)); break; case Cell.CELL_TYPE_STRING: rowMap.put(firstRow.getCell(k).getStringCellValue(), cell.getStringCellValue()); break; case Cell.CELL_TYPE_BLANK: break; default: rowMap.put(firstRow.getCell(k).getStringCellValue(), cell.toString()); break; } } // 是否空行 if (rowMap.size() > 0) { resultList.add(rowMap); } } } catch (IOException e) { e.printStackTrace(); } return resultList; } }