jxl操作Excel

Java高級羣:224651178

//jxl操作Excel工具類 包含導入導出代碼 package com.wzpmt.excel.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.Colour; import jxl.read.biff.BiffException; import jxl.write.WritableCellFormat; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; //import org.apache.struts2.ServletActionContext; public class ExcelUtils { /** * 獲取表頭的格式 * @return */ public static WritableCellFormat getHeadCellFormat(){ WritableCellFormat wcfHead=new WritableCellFormat(); try { wcfHead.setBorder(Border.ALL, BorderLineStyle.THIN); wcfHead.setAlignment(Alignment.CENTRE); wcfHead.setBackground(Colour.YELLOW2); } catch (Exception e) { e.printStackTrace(); } return wcfHead; } /** * 獲取紅色的單元格式 * @return */ public static WritableCellFormat getRedCellFormat(){ WritableCellFormat wcRed = new WritableCellFormat(); try { wcRed.setBorder(Border.ALL, BorderLineStyle.THIN); wcRed.setAlignment(Alignment.CENTRE); wcRed.setBackground(Colour.RED); } catch (Exception e) { e.printStackTrace(); } return wcRed; } /** * 獲取WritableWorkbook對象,讓調用者寫excel * @return */ public static WritableWorkbook getWritableWorkbook(){ ByteArrayOutputStream os =new ByteArrayOutputStream(); WritableWorkbook workBook =null; try { workBook = Workbook.createWorkbook(os); } catch (IOException e) { e.printStackTrace(); } return workBook; } /** * 寫入WritableWorkBook後關閉它,並將寫入的excel以流的形式返回 * @param workBook * @param os * @return */ public static ByteArrayInputStream writeCloseWorkbook(WritableWorkbook workBook,ByteArrayOutputStream os){ try { workBook.write(); } catch (IOException e1) { e1.printStackTrace(); }finally{ if(workBook!=null){ try { workBook.close(); } catch (IOException e) { e.printStackTrace(); } } } return new ByteArrayInputStream(os.toByteArray()); } /** * 寫excel的表頭 * @param headNames * @param sheet * @return */ public static WritableSheet writeExcelHead(String[] headNames,WritableSheet sheet){ try { jxl.write.WritableCell wcell=null; for(int i=0;i<headNames.length;i++){ wcell = new jxl.write.Label(i,0,headNames[i],getHeadCellFormat()); sheet.addCell(wcell); } } catch (Exception e) { e.printStackTrace(); } return sheet; } /** * 將已經寫好的excel以流的形式寫出 * @param excelName * @param is */ public static void ExcelExport(String excelName,InputStream is,HttpServletResponse response){ String expFileName=excelName; try { expFileName = new String(expFileName.getBytes("gbk"),"iso8859-1"); //HttpServletResponse response= ServletActionContext.getResponse(); response.setContentType("application/ms-excel; charset=UTF-8"); response.setHeader("Content-Disposition","attachment;filename="+ expFileName+".xls"); InputStream inputStream=is; OutputStream os = null; response.setContentLength(inputStream.available()); os = response.getOutputStream(); int iBytesRead = 0; byte[] buffer = new byte[8192]; BufferedInputStream bis = new BufferedInputStream(inputStream, 8192); BufferedOutputStream bos = new BufferedOutputStream(os, 8192); while ((iBytesRead = bis.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, iBytesRead); } bos.flush(); bos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 根據文件獲取Workbook對象,用於導入 * @param file * @return * @throws IOException * @throws BiffException * @throws Exception */ public static Workbook getWorkBookForImport(File file) throws Exception{ InputStream is = new FileInputStream(file); Workbook workBook = Workbook.getWorkbook(is); return workBook; } public static void closeWorkBookForImport(Workbook workBook){ if(workBook!=null){ try { workBook.close(); } catch (Exception e) { e.printStackTrace(); } } } }

Java高級羣:224651178


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章