读书人

ExcelUtil根本工具类

发布时间: 2012-12-27 10:17:10 作者: rapoo

ExcelUtil——基本工具类

public class ExcelUtil {
?
?private static final Log log = LogFactory.getLog(ExcelUtil.class);//日志
?
?/**
? * 建Excel象
? * @param excelBean
? * @return
? */
?public static HSSFWorkbook creatExcel(ExcelBean excelBean){
??HSSFWorkbook wb = new HSSFWorkbook(); //建Excel工作薄
??//在Excel工作薄中建一工作表,默缺省值
//??HSSFSheet sheet = wb.createSheet();
??????? HSSFSheet sheet = wb.createSheet(excelBean.getSheetName());
??????? //置工作表各列度
?????? // setColumnWidth(sheet, excelBean.getWidth());
??????? //置字,式
??????? //HSSFFont font = setFont(wb, "宋");
??????? //HSSFCellStyle style = setCellStyle(wb, font);
??????? //置第一行
??????? List<String> titleList = excelBean.getTitleList();
??????? setRowValue(sheet, titleList, 0); //在索引0的位置建行(最端的行)
??????? //置行
??????? List dataList = excelBean.getDataList();
??????? if(dataList != null){
??????? ?for(int i=0; i<dataList.size(); i++){??????? ??
??????? ??List rowList = (List) dataList.get(i);
??????? ??setRowValue(sheet, rowList, i+1); //在索引1的位置建行(第2行)??????? ??
??????? ?}
??????? }
??????? return wb;
?}
?
?/**
? * 置行值
? * @param sheet
? * @param List
? * @return
? */
?public static HSSFSheet setRowValue(HSSFSheet sheet, List<String> list,int rowNo){
??if(list != null){
???//在索引rowNo的位置建行
???HSSFRow rowTitle = sheet.createRow(rowNo);
???for(int i= 0 ; i<list.size();i++){
????String title = list.get(i);
????//索引0的位置始建元格(左上端)
????HSSFCell cell = rowTitle.createCell((short)i);
????cell.setCellValue(title); //置元格容
???}
??}
??return sheet;
?}
?
?/**
? * 下Excel
? * @param wb
? * @param filename MemberList.xls
? * @param response
? * @return
? */
?public static boolean downloadExcel(HSSFWorkbook wb, String filename, HttpServletResponse response){
??boolean flag = true;??
??//置入流
??OutputStream output = null;
??try {
//???置型
???response.setHeader("Content-disposition", "attachment;filename="+new String(filename.getBytes("gbk"),"ISO-8859-1"));
???response.setContentType("application/x-msdownload");
???
???output = response.getOutputStream();???
???wb.write(output);//出到中
???output.flush();
???log.info("下Excel:" + filename + ", 成功!");
??} catch (Exception e) {
???flag = false;
???log.info("下Excel:" + filename + ", 失!");
???e.printStackTrace();
??} finally{
???try {
????output.close();
???} catch (IOException e) {
????e.printStackTrace();
???}
??}
??return flag;??
?}
?
?/**
? * 下Excel
? * add by zhangde 2011-3-17
? * @param wb
? * @param filename MemberList.xls
? * @param response
? * @return
? */
?public static boolean downloadExcelEncode(String encoding, HSSFWorkbook wb, String filename, HttpServletResponse response){
??boolean flag = true;??
??//置入流
??OutputStream output = null;
??try {
//???置型,用utf-8,可能会出现高位(前面)被截。gbk在简体操作系统中不会。
//???response.setHeader("Content-disposition", "attachment;filename="+new String(filename.getBytes(encoding),"ISO-8859-1"));
???response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(filename, encoding));
???System.out.println("filename=" + filename);
???System.out.println("URLEncoderfilename=" + URLEncoder.encode(filename, encoding));
???response.setContentType("application/x-msdownload");
???
???output = response.getOutputStream();???
???wb.write(output);//出到中
???output.flush();
???log.info("下Excel:" + filename + ", 成功!");
??} catch (Exception e) {
???flag = false;
???log.info("下Excel:" + filename + ", 失!");
???e.printStackTrace();
??} finally{
???try {
????output.close();
???} catch (IOException e) {
????e.printStackTrace();
???}
??}
??return flag;??
?}
?
?
?/**
? * 下Excel
? * @param wb
? * @param filename MemberList.xls
? * @param response
? * @param encoding 例如 gbk, utf-8
? * @return
? */
?public static boolean downloadExcel(String encoding, HSSFWorkbook wb, String filename, HttpServletResponse response){
??boolean flag = true;??
??//置入流
??OutputStream output = null;
??String name = "";
??try {
//???置型
???name = FileUtil.toEncodeString(filename, encoding);?// 文件名
???response.setHeader("Content-disposition", "attachment;filename=" + name);
???response.setContentType("application/x-msdownload");
???output = response.getOutputStream();???
???wb.write(output);//出到中
???output.flush();
???log.info("下Excel:" + filename + ", 成功!");
??} catch (Exception e) {
???flag = false;
???log.info("下Excel:" + filename + ", 失!");
???e.printStackTrace();
??} finally{
???try {
????output.close();
???} catch (IOException e) {
????e.printStackTrace();
???}
??}
??return flag;??
?}
?
?/**
? * 入文件(excel)
? * @param path?? 路+案名
? * @param line?? 第始
? * @param rowLen 到第列束
? * @return list
? * @throws Exception
? */
?public static List<Object> importData(InputStream inputStream,int atRow, int line, int rowLen) throws Exception{?
??List<Object> tmpList = new ArrayList<Object>();?
??POIFSFileSystem pfs = new POIFSFileSystem(inputStream);
??HSSFWorkbook wb = new HSSFWorkbook(pfs);
??HSSFSheet sheet = wb.getSheetAt(0);
??HSSFRow row = null;
??try{??
???int totalRow = sheet.getLastRowNum(); //excel料行
???if(totalRow>0){ //判excel是否空
????for(int i = atRow ; i <=totalRow; i++){
?????? ?List<Object> rowList = new ArrayList<Object>();?
?????? ?row = sheet.getRow(i);
?????? ?if(row!=null){ //判行是否空
?????? ??for(int j = line; j < rowLen; j++){?
???????HSSFCell cell = row.getCell((short) j);
???????if(cell == null){ //判列是否空
????????rowList.add("");
???????}else{
????????int ctype = cell.getCellType();
????????switch(ctype){
?????????// 理string部份
?????????case HSSFCell.CELL_TYPE_BLANK:
??????????rowList.add("");
?????????break;
?????????
?????????case HSSFCell.CELL_TYPE_STRING:
??????????rowList.add(cell.getRichStringCellValue().getString().trim());
?????????break;
?????????
?????????//理number部份
?????????case HSSFCell.CELL_TYPE_NUMERIC:
??????????
??????????if (HSSFDateUtil.isCellDateFormatted(cell)) { // 是否日期型
???????????System.out.print( cell.getDateCellValue());
???????????//date格式
???????????rowList.add(cell.getDateCellValue());
???????????? }else{
???????????? ?
???????????double tmpdouble = cell.getNumericCellValue();
???????????if(tmpdouble - (int)tmpdouble < Double.MIN_VALUE) {
????????????
????????????//int
????????????rowList.add(Integer.toString((int)tmpdouble));
???????????}else{
????????????
????????????//double
????????????rowList.add(Double.toString(cell.getNumericCellValue()));
???????????}
???????????? }
?????????break;
?????????
?????????//理boolean部份
?????????case HSSFCell.CELL_TYPE_BOOLEAN:?
??????????rowList.add(cell.getBooleanCellValue());
?????????break;
????????}
???????}?
??????? ?}
??????? ?tmpList.add(rowList);
?????? ?}
?????? }
???}
??}catch(Exception e){
???e.printStackTrace();
???throw e;
??}
???? return tmpList;
?}

?/**
? * 入文件(excel)包含空行
? * @param path?? 路+案名
? * @param line?? 第始
? * @param rowLen 到第列束
? * @return list
? * @throws Exception
? */
?public static List<Object> importData1(InputStream inputStream,int atRow, int line, int rowLen) throws Exception{?
??List<Object> tmpList = new ArrayList<Object>();?
??POIFSFileSystem pfs = new POIFSFileSystem(inputStream);
??HSSFWorkbook wb = new HSSFWorkbook(pfs);
??HSSFSheet sheet = wb.getSheetAt(0);
??HSSFRow row = null;
??try{??
???int totalRow = sheet.getLastRowNum(); //excel料行
???if(totalRow>0){ //判excel是否空
????for(int i = atRow ; i <=totalRow; i++){
?????? ?List<Object> rowList = new ArrayList<Object>();?
?????? ?row = sheet.getRow(i);
?????? ?if(row!=null){ //判行是否空
?????? ??for(int j = line; j < rowLen; j++){?
???????HSSFCell cell = row.getCell((short) j);
???????if(cell == null){ //判列是否空
????????rowList.add("");
???????}else{
????????int ctype = cell.getCellType();
????????switch(ctype){
?????????// 理string部份
?????????case HSSFCell.CELL_TYPE_BLANK:
??????????rowList.add("");
?????????break;
?????????
?????????case HSSFCell.CELL_TYPE_STRING:
??????????rowList.add(cell.getRichStringCellValue().getString().trim());
?????????break;
?????????
?????????//理number部份
?????????case HSSFCell.CELL_TYPE_NUMERIC:
??????????
??????????if (HSSFDateUtil.isCellDateFormatted(cell)) { // 是否日期型
???????????System.out.print( cell.getDateCellValue());
???????????//date格式
???????????rowList.add(cell.getDateCellValue());
???????????? }else{
???????????? ?
???????????double tmpdouble = cell.getNumericCellValue();
???????????if(tmpdouble - (int)tmpdouble < Double.MIN_VALUE) {
????????????
????????????//int
????????????rowList.add(Integer.toString((int)tmpdouble));
???????????}else{
????????????
????????????//double
????????????rowList.add(Double.toString(cell.getNumericCellValue()));
???????????}
???????????? }
?????????break;
?????????
?????????//理boolean部份
?????????case HSSFCell.CELL_TYPE_BOOLEAN:?
??????????rowList.add(cell.getBooleanCellValue());
?????????break;
????????}
???????}?
??????? ?}
?????? ?}else{
?????? ??for(int k = line; k < rowLen; k++){?
?????? ???rowList.add("");
?????? ??}
?????? ?}
?????? ?tmpList.add(rowList);
?????? }
???}
??}catch(Exception e){
???e.printStackTrace();
???throw e;
??}
???? return tmpList;
?}
?
}

读书人网 >编程

热点推荐