Apache POI 操作Excel

taozihk發表於2016-05-17

認識POI

Apache POI是Apache軟體基金會的開放原始碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。

結構:
HSSF - 提供讀寫Microsoft Excel格式檔案的功能。
XSSF - 提供讀寫Microsoft Excel OOXML格式檔案的功能。
HWPF - 提供讀寫Microsoft Word格式檔案的功能。
HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。
HDGF - 提供讀寫Microsoft Visio格式檔案的功能。

在開源世界中,有兩套比較有影響的API可 供開發者使用,一個是POI,一個是JXL,其中功能相對POI比較弱一點,所以我們日常開發中用到更多的往往是POI,在這裡我們也只對POI做一個深入的使用講解,並給出一個導Excel的工具類,適用任何列表的匯出。


POI的jar包下載地址


簡易的POI寫入資料(認識API)


1:建立新HSSFWorkbook物件

        HSSFWorkbook  wb = new HSSFWorkbook();

2:建立新的sheet物件

         HSSFSheet  sheet  wb.createSheet("new sheet");

3:在sheet裡建立一行,引數為行號

        HSSFRow r ow sheet.createRow((short)0);

4:在row裡新建新cell(單元格)引數為列號

       4.1、給第一列設定一個整型值

                HSSFCell cell  =  row.createCell((short)0);

                cell.setCellValue(1);

       4.2、給第二列設定一個浮點型值

                HSSFCell  cell=  row.createCell((short)1);

                cell.setCellValue(1.2);

       4.3、給第三列設定一個字串型值

         HSSFCell  cell =  row.createCell((short)2);

         cell.setCellValue(“test”);

4.4、給第四列設定一個布林型值

         HSSFCell  cell  row.createCell((short)3);

         cell.setCellValue(true);

4.5、給第五列設定一個日期型值

              設立新的cell樣式 :

                      HSSFCellStyle  cellStyle  =  wb.createCellStyle();

              設定cell樣式為定製的日期格式:

                     cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyy年MM月dd日"));

                     HSSFCell cell  =  row.createCell((short)4);

                     cell.setCellValue(new Date());

                     cell.setCellStyle(cellStyle);

5:往磁碟寫入excel檔案

               FileOutputStream os = new FileOutputStream("d:\\test.xls");
               wb.write(os);
               os.close();


單元格樣式設定


HSSFCellStyle style = workbook.createCellStyle();

單元格的顔色有前景色和背景色。 

style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

style.setFillBackgroundColor(HSSFColor.SKY_BLUE.index);

POI提供的顏色類:HSSFColor


設定顔色時,用這些子類的靜態常量「index」作為引數。


如果這些顔色還不夠你用的話,怎麼設定自己想要的顔色呢

填充模式

指定填充模式的話,使用「HSSFCellStyle」類的「setFillPattern」方法。 

style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

說明
NO_FILL No background
SOLID_FOREGROUND Solidly filled
FINE_DOTS Small fine dots
ALT_BARS Wide dots
SPARSE_DOTS Sparse dots
THICK_HORZ_BANDS Thick horizontal bands
THICK_VERT_BANDS Thick vertical bands
THICK_BACKWARD_DIAG Thick backward facing diagonals
THICK_FORWARD_DIAG Thick forward facing diagonals
BIG_SPOTS Large spots
BRICKS Brick-like layout
THIN_HORZ_BANDS Thin horizontal bands
THIN_VERT_BANDS Thin vertical bands
THIN_BACKWARD_DIAG Thin backward diagonal
THIN_FORWARD_DIAG Thin forward diagonal
SQUARES Squares
DIAMONDS Diamonds


設定邊框的樣式

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);

設定單元格內容位置

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);



文字樣式設定


HSSFFont font = workbook.createFont();

設定文字顏色
font.setColor(HSSFColor.VIOLET.index);

設定字型大小
font.setFontHeightInPoints((short) 12);

設定字型粗細度
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);


把字型應用到當前的樣式
style.setFont(font);


一個通用的工具類

<pre name="code" class="java"><pre name="code" class="java">public class ExportExcelUtil<T>{
	/**
	 * 這是一個通用的方法,利用了JAVA的反射機制,可以將放置在JAVA集合中並且符號一定條件的資料以EXCEL 的形式輸出到指定IO裝置上
	 * 
	 * @param title
	 * 
	 *            表格標題名
	 * 
	 * @param headers
	 * 
	 *            表格屬性列名陣列
	 * 
	 * @param dataset
	 * 
	 *            需要顯示的資料集合,集合中一定要放置符合javabean風格的類的物件。此方法支援的
	 *            javabean屬性的資料型別有基本資料型別及String,Date,byte[](圖片資料)
	 * 
	 * @param out
	 * 
	 *            與輸出裝置關聯的流物件,可以將EXCEL文件匯出到本地檔案或者網路中
	 * 
	 * @param pattern
	 * 
	 *            如果有時間資料,設定輸出格式。預設為"yyy-MM-dd"
	 * 
	 */

	@SuppressWarnings("unchecked")
	public byte[] exportExcel(String title, String[] headers,Collection<T> dataset, String pattern){
		// 宣告一個工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFFont font3 = workbook.createFont();
		// 生成一個表格
		// 生成一個樣式
		HSSFCellStyle style = workbook.createCellStyle();
		// 設定這些樣式
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一個字型
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字型應用到當前的樣式
		style.setFont(font);
		// 生成並設定另一個樣式
		HSSFCellStyle style2 = workbook.createCellStyle();
		style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 生成另一個字型
		HSSFFont font2 = workbook.createFont();
		font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		// 把字型應用到當前的樣式
		style2.setFont(font2);
		HSSFSheet sheet = null;
		HSSFRow row = null;
		int index = 0;
		int sheetnum = 0;
		Iterator<T> it = dataset.iterator();
		// 產生表格標題行
		sheet = workbook.createSheet(title + sheetnum);
		sheet.setDefaultColumnWidth((short) 15);
		row = sheet.createRow(0);
		for (short i = 0; i < headers.length; i++){
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
		while (it.hasNext()){
			index++;
			if (index > 50000){
				index = 0;
				++sheetnum;
				sheet = workbook.createSheet(title + sheetnum);
				sheet.setDefaultColumnWidth((short) 15);
				row = sheet.createRow(0);
				for (short i = 0; i < headers.length; i++){
					HSSFCell cell = row.createCell(i);
					cell.setCellStyle(style);
					HSSFRichTextString text = new HSSFRichTextString(headers[i]);
					cell.setCellValue(text);
				}
			}
			row = sheet.createRow(index);
			T t = (T) it.next();
			// 利用反射,根據javabean屬性的先後順序,動態呼叫getXxx()方法得到屬性值
			Field[] fields = t.getClass().getDeclaredFields();
			for (short i = 0; i < fields.length; i++){
				HSSFCell cell = row.createCell(i);
				cell.setCellStyle(style2);
				Field field = fields[i];
				String fieldName = field.getName();
				String getMethodName = "get"+ fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);
				try{
					Class tCls = t.getClass();
					Method getMethod = tCls.getMethod(getMethodName,new Class[] {});
					Object value = getMethod.invoke(t, new Object[] {});
					// 判斷值的型別後進行強制型別轉換
					String textValue = null;
					if (value instanceof Integer){
						int intValue = (Integer) value;
						cell.setCellValue(intValue);
					}
					else if (value instanceof Float){
						float fValue = (Float) value;
						textValue = new HSSFRichTextString(String.valueOf(fValue)).toString();
						cell.setCellValue(textValue);
					}
					else if (value instanceof Double){
						double dValue = (Double) value;
						textValue = new HSSFRichTextString(String.valueOf(dValue)).toString();
						cell.setCellValue(textValue);
					}
					else if (value instanceof Long){
						long longValue = (Long) value;
						cell.setCellValue(longValue);
					}
					if (value instanceof Boolean){
						boolean bValue = (Boolean) value;
						textValue = "true";
						if (!bValue){
							textValue = "false";
						}
					}
					else if (value instanceof Date){
						Date date = (Date) value;
						if ("".equals(pattern)){
							pattern = "yyy-MM-dd";
						}
						SimpleDateFormat sdf = new SimpleDateFormat(pattern);
						textValue = sdf.format(date);
					}
					else{
						if (null == value || "".equals(value)){
							value = "";
						}
						else{
							textValue = value.toString();
						}
					}
					// 如果不是圖片資料,就利用正規表示式判斷textValue是否全部由數字組成
					if (textValue != null){
						Pattern p = Pattern.compile("^//d+(//.//d+)?$");
						Matcher matcher = p.matcher(textValue);
						if (matcher.matches()){
							// 是數字當作double處理
							cell.setCellValue(Double.parseDouble(textValue));
						}
						else{
							HSSFRichTextString richString = new HSSFRichTextString(textValue);
							font3.setColor(HSSFColor.BLUE.index);
							richString.applyFont(font3);
							cell.setCellValue(richString);
						}
					}
				}
				catch (Exception e){
					e.printStackTrace();
				}
			}
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try{
			workbook.write(baos);
			baos.flush();
		}
		catch (IOException e){
			e.printStackTrace();
		}
		return baos.toByteArray();
	}
}



匯入excel
<pre name="code" class="java">public class ExcelReader {
    /**
     * 讀取“.xls”格式使用  import org.apache.poi.hssf.usermodel.*;包的內容,例如:HSSFWorkbook
     * 讀取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的內容,例如:XSSFWorkbook
     * 讀取兩種格式使用    import org.apache.poi.ss.usermodel.*    包的內容,例如:Workbook
     * @param filePath
     * @return
     */
    public static List<String[]> readExcel(String filePath){
        String fullPath = ExcelReader.class.getClassLoader().getResource(filePath).getPath();
        Workbook wb = null;
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(fullPath));
            wb = WorkbookFactory.create(in); 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        List<String[]> list = new ArrayList<String[]>();
        for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++){
            Sheet st = wb.getSheetAt(sheetIndex);
            // 第一行為標題,不取
            for (int rowIndex = 0; rowIndex <= st.getPhysicalNumberOfRows(); rowIndex++){
                Row row = st.getRow(rowIndex);
                if (row == null) {
                    continue;
                }
                String[] cells = cellArray(row);
                list.add(cells);
            }
        }
        return list;
    }
    
    private static String[] cellArray(Row row) {
        String[] cellArray = new String[row.getPhysicalNumberOfCells()];
        for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
            String value = "";
            Cell cell = row.getCell(index);
            if (cell != null) {
                switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_STRING :
                        value = cell.getStringCellValue();
                        break;
                    case HSSFCell.CELL_TYPE_NUMERIC :
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            Date date = cell.getDateCellValue();
                            if (date != null) {
                                value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                            } else {
                                value = "";
                            }
                        } else {
                            value = new DecimalFormat("0.00").
                                    format(cell.getNumericCellValue());
                        }
                        break;
                    case HSSFCell.CELL_TYPE_FORMULA :
                        try{
                            value = String.valueOf(cell.getNumericCellValue());
                        } catch(IllegalStateException e){
                            value = String.valueOf(cell.getRichStringCellValue());
                        }
                        break;
                    case HSSFCell.CELL_TYPE_BLANK :
                        break;
                    case HSSFCell.CELL_TYPE_ERROR :
                        value = "";
                        break;
                    case HSSFCell.CELL_TYPE_BOOLEAN :
                        value = (cell.getBooleanCellValue() == true ? "Y" : "N");
                        break;
                    default :
                        value = "";
                }
            } else {
                value = "";
            }
            cellArray[index] = value;
        }
        return cellArray;
    }
    
    public static void main(String[] args) {
        List<String[]> list = readExcel("excel/20160517.xls");
        for(String[] arr:list){
            System.out.println(Arrays.toString(arr));
        }
    }
}





相關文章