圖片爲excel數據html
package com.msi.excel;java
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook;sql
import com.msi.dao.DBConnection; import com.msi.dao.DiaryDao; import com.msi.model.Diary; import com.msi.model.PageBean;apache
/*字體
public class ReadExcel {excel
// 將List寫入Excel ,然後經過在網頁上使用,便於導出excel public static String TransXLSFileLot(List<Diary> list, String path) { String result = ""; String header[] = { "diaryId", "title", "content", "typeId", "releaseDate" }; Workbook workbook = new HSSFWorkbook(); CellStyle style1 = workbook.createCellStyle(); style1.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); style1.setBorderLeft(HSSFCellStyle.BORDER_THIN); style1.setBorderRight(HSSFCellStyle.BORDER_THIN); style1.setBorderTop(HSSFCellStyle.BORDER_THIN); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成一個字體 Font font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字體應用到當前的樣式 style1.setFont(font2); Sheet sheet = workbook.createSheet("Test"); Row rowHeader = sheet.createRow(0); for (int i = 0; i < header.length; i++) { Cell cell = rowHeader.createCell(i); // 行添加單元格 cell.setCellStyle(style1); cell.setCellValue(header[i]); } for (int j = 0; j < list.size(); j++) { Diary diary = list.get(j); Row rowBody = sheet.createRow(j + 1); rowBody.createCell(0).setCellValue(diary.getDiaryId()); rowBody.createCell(1).setCellValue(diary.getTitle()); rowBody.createCell(2).setCellValue(diary.getContent()); rowBody.createCell(3).setCellValue(diary.getTypeId()); rowBody.createCell(4).setCellValue( com.msi.util.DateUtil.formatDate(diary.getReleaseDate(), "yyyy-MM-dd")); } OutputStream os = null; BufferedOutputStream bos = null; try { File file = new File(path); if (file.exists()) { file.delete(); System.err.print("存在就先刪除,然後在寫入。。。。"); } else { file.createNewFile(); } os = new FileOutputStream(file); bos = new BufferedOutputStream(os); workbook.write(bos); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); result = e.getMessage(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); result = e.getMessage(); } finally { try { bos.close(); os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } // 參考網址 http://www.2cto.com/kf/201108/100774.html public static String getValue(Cell cell) { SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd"); String value = ""; switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: // 數值型 分爲String 類型和int 型的 if (DateUtil.isCellDateFormatted(cell)) { // 若是是date類型則,獲取該cell的date值 value = dateformat.format( DateUtil.getJavaDate(cell.getNumericCellValue())) .toString(); } else { cell.setCellType(Cell.CELL_TYPE_STRING); String temp = cell.getStringCellValue(); if (temp.indexOf(".") > -1) { value = String.valueOf(new Double(temp)).trim(); } else { value = temp.trim(); } } break; case Cell.CELL_TYPE_STRING: // 字符串型 // value = cell.getRichStringCellValue().toString(); value = cell.getStringCellValue().trim(); break; case Cell.CELL_TYPE_BOOLEAN:// 布爾 value = " " + cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_FORMULA:// 公式型 // 讀公式計算值 value = String.valueOf(cell.getNumericCellValue()); if (value.equals("NaN")) {// 若是獲取的數據值爲非法值,則轉換爲獲取字符串 value = cell.getRichStringCellValue().toString(); } break; /* 此行表示該單元格值爲空 */ case Cell.CELL_TYPE_BLANK: // 空值 value = ""; break; case Cell.CELL_TYPE_ERROR: // 故障 value = ""; break; default: value = cell.getStringCellValue().trim(); break; } return value; } // 讀取Excel的表頭 public static String[] getExcelHeader(String fileName) { InputStream is = null; BufferedInputStream bis = null; String[] title = null; try { is = new FileInputStream(new File(fileName)); bis = new BufferedInputStream(is); Workbook workbook = new HSSFWorkbook(bis); Sheet sheet = workbook.getSheet("Test"); Row row = sheet.getRow((int) 0); int colNum = row.getPhysicalNumberOfCells(); // 標題總列數 title = new String[colNum]; for (int i = 0; i < colNum; i++) { title[i] = getValue(row.getCell(i)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bis.close(); is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return title; } // 將Excel數據導入List中,此時的 public static List<List<String>> getExcel(String fileName) { List<List<String>> list = new ArrayList<List<String>>(); try { FileInputStream fis = new FileInputStream(new File(fileName)); Workbook wb = new HSSFWorkbook(fis); // 03,xls // XSSFWorkbook wb=new XSSFWorkbook(fis); //07,xlsx // HSSFWorkbook wb=new HSSFWorkbook(fis); //03,xls int sheetNum = wb.getNumberOfSheets(); for (int i = 0; i < sheetNum; i++) { // 遍歷工做簿 Sheet sheet = null; sheet = wb.getSheetAt(i); // 03,xls // XSSFSheet sheet = wb.getSheetAt(i); //07,xlsx // HSSFSheet sheet = wb.getSheetAt(i); //03,xls int rowNum = sheet.getPhysicalNumberOfRows(); // .getLastRowNum(); for (int j = 0; j < rowNum; j++) { // 在這裡修改,將j<= 改爲了j< ; List<String> vecRow = new ArrayList<String>(); Row row = sheet.getRow(j); // XSSFRow rRow = sheet.getRow(j); //07,xlsx // HSSFRow rRow = sheet.getRow(j); //03,xls int cellNum = row.getLastCellNum(); // 表示行的列數 for (int k = 0; k < cellNum; k++) { // k表示遞增 的列數 vecRow.add(row.getCell(k).toString()); } list.add(vecRow); } } } catch (Exception e) { e.printStackTrace(); } return list; } // excel轉化成List是主要是先轉成List<String> ,而後將其拆分,而後歸屬到JavaBean中 public static List<Diary> getTranListFromExcel() throws Exception { List<List<String>> listA = getExcel("C:\\asdfg1.xls"); List<Diary> listC = new ArrayList<Diary>(); // 從1開始時爲了將表頭去掉 for (int i = 1; i < listA.size(); i++) { List<String> listB = listA.get(i); Diary diary = new Diary(); if (listB.get(0).indexOf(".") > -1) { int index = listB.get(0).indexOf("."); diary.setDiaryId(Integer.parseInt(listB.get(0).substring(0, index))); } else { diary.setDiaryId(Integer.parseInt(listB.get(0))); } diary.setTitle(listB.get(1)); diary.setContent(listB.get(2)); if (listB.get(3).indexOf(".") > -1) { int index = listB.get(3).indexOf("."); diary.setTypeId(Integer.parseInt(listB.get(3).substring(0, index))); } else { diary.setTypeId(Integer.parseInt(listB.get(3))); } diary.setReleaseDate(com.msi.util.DateUtil.formatString( listB.get(4), "yyyy-MM-dd")); listC.add(diary); } return listC; } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Connection conn = DBConnection.getConnection(); PageBean pageBean = new PageBean(1, 100); Diary s_diary = new Diary(); List<Diary> list = new DiaryDao().diaryList(pageBean, s_diary); TransXLSFileLot(list, "C:\\asdfg1.xls"); String[] title = getExcelHeader("C:\\asdfg1.xls"); for (int i = 0; i < title.length; i++) { System.out.print(title[i] + " "); } List<List<String>> listA = getExcel("C:\\asdfg1.xls"); for (int i = 0; i < listA.size(); i++) { System.out.println(listA.get(i)); } List<Diary> listC = getTranListFromExcel(); for (int i = 0; i < listC.size(); i++) { Diary d = listC.get(i); System.out.println(i + 1 + " " + d.getDiaryId() + " " + d.getTypeId() + " " + d.getTitle() + " " + d.getReleaseDate() + " " + d.getContent()); System.err.println("-------------"); } }
}code