package com.dongxi.goods.other; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.*; import java.util.ArrayList; import java.util.List; /**讀取excel表格的數據進行處理 * 將每行放到一個list中,然后每行的每列放入到一個list中,這里再根據自己需求去對表中數據進行處理: * @Author: huangdx_gx * @Decscription: poi解析Excel * @date: 2018/3/6 - 10:07 * @see: com.dongxi.goods.other */ public class ExcelReaderOrWriteUtil { /** * 根據fileType不同讀取Excel文件 * @param path * @return */ public static List<List<String>> readExcel(String path){ String fileType = path.substring(path.lastIndexOf(".")+1);//獲取以.開始位置+1到字符串結尾之間的串,即拿到文件的后綴名 //return a list contains many list List<List<String>> lists = new ArrayList<List<String>>();//返回一個lists里面包含很多個list //讀取Excel文件 InputStream is = null; try { is = new FileInputStream(path); //獲取工作薄 Workbook wb = null; if (fileType.equals("xls")){ wb = new HSSFWorkbook(is); }else if (fileType.equals("xlsx")){ wb = new XSSFWorkbook(is); }else { return null; } //讀取第一個工作頁sheet Sheet sheet = wb.getSheetAt(0); //第一行為標題 for (Row row : sheet){ ArrayList<String> list = new ArrayList<String>(); for (Cell cell : row){ //根據不同類型轉化成字符串 cell.setCellType(Cell.CELL_TYPE_STRING); list.add(cell.getStringCellValue()); } lists.add(list); } } catch (IOException e) { e.printStackTrace(); }finally { if (is !=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return lists; } /** * 創建Excel.xls * @param lists 需要寫入xls的數據 * @param title 列標題 * @param name 文件名 * @return */ public static Workbook crearExcel (List<List<String>> lists,String title, String name){ System.out.println(lists); String[] titles = title.split(","); //創建工作薄 Workbook wb = new HSSFWorkbook(); //創建第一個sheet(頁),並命名 Sheet sheet = wb.createSheet(name); //手動設置列寬。第一個參數表示要為第幾列設;第二個參數表示列的寬度,n為列高的像素數。 for (int i= 0;i<titles.length;i++){ sheet.setColumnWidth((short) i,(short) (35.7*150)); } //創建第一行 Row row = sheet.createRow((short) 0); //創建兩種單元格格式 CellStyle cs = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); //創建兩種字體 Font f = wb.createFont(); Font f2= wb.createFont(); //創建第一種字體樣式(用於列名) f.setFontHeightInPoints((short) 10); f.setColor(IndexedColors.BLACK.getIndex()); f.setBoldweight(Font.BOLDWEIGHT_BOLD); //創建第二種字體樣式(用於值) f2.setFontHeightInPoints((short)10); f2.setColor(IndexedColors.BLACK.getIndex()); //設置第一種單元格的樣式(用於列) cs.setFont(f); cs.setBorderLeft(CellStyle.BORDER_THIN); cs.setBorderRight(CellStyle.BORDER_THIN); cs.setBorderTop(CellStyle.BORDER_THIN); cs.setBorderBottom(CellStyle.BORDER_THIN); cs.setAlignment(CellStyle.ALIGN_CENTER);//居中 //設置第二種單元格的樣式(用於值) cs2.setFont(f2); cs2.setBorderLeft(CellStyle.BORDER_THIN); cs2.setBorderRight(CellStyle.BORDER_THIN); cs2.setBorderTop(CellStyle.BORDER_THIN); cs2.setBorderBottom(CellStyle.BORDER_THIN); cs2.setAlignment(CellStyle.ALIGN_CENTER); //設置列名 for (int i=0; i<titles.length;i++){ Cell cell = row.createCell(i); cell.setCellValue(titles[i]); cell.setCellStyle(cs); } if (lists == null || lists.size() == 0){ return wb; } //設置每行每列的值 for (short i=1; i<=lists.size();i++){//總行數 //row行,cell 方格,row 和cell 都是從0開始計數的 //創建一行,在頁sheet上 Row row1 = sheet.createRow((short) i);//第一行 for (short j= 0;j<titles.length;j++){//第一行有多少列,然后在每列的單元格上set如數據 //在row行上創建一個方格 Cell cell = row1.createCell(j); cell.setCellValue(lists.get(i-1).get(j));//list里面裝list,先拿到第一層的list,再拿第二層的list的一行的列的值 cell.setCellStyle(cs2);//把剛才配置好的表格長寬字體加入表格的配置 } } return wb; } /** * 保存到本地硬盤 * @param workbook */ public static void saveExcel( Workbook workbook){ FileOutputStream fos = null; try { fos = new FileOutputStream("F:\\user2.xls"); workbook.write(fos); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args ){ String path = "F:\\user.xls"; String title ="用戶名,登錄名,登錄密碼,用戶號碼,用戶地址"; List<List<String>> lists = readExcel(path); Workbook workbook = crearExcel(lists,title,"用戶表"); saveExcel(workbook); for (List<String> list : lists){ for (String strs : list){ System.out.print(strs+" "); } System.out.println(); } } }
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。