导出Excel,按照符合jxl的方式导出

来源:互联网 发布:压缩文件夹 linux 编辑:程序博客网 时间:2024/06/07 00:32
  1. public void outPutExcel(List<InvoiceRequisitionSo> result,HttpServletResponse response ){   
  2.     OutputStream os = null;   
  3.     try{   
  4.         os = response.getOutputStream();   
  5.     }catch (IOException el){   
  6.   
  7.     }   
  8.     response.reset();   
  9.     response.setHeader("Content-disposition""attachment; filename=InvoiceRequisitionListToExcel.xls");   
  10.     response.setContentType("application/msexcel");   
  11.     WritableWorkbook wwb=null;   
  12.     WritableSheet ws=null;   
  13.     WritableCellFormat cellFormat = new WritableCellFormat();   
  14.     WritableCellFormat cellFormatContent = new WritableCellFormat();   
  15.     WritableFont font= new WritableFont(WritableFont.createFont("宋体"),10, WritableFont.NO_BOLD);   
  16.     WritableFont font1= new WritableFont(WritableFont.createFont("宋体"),10, WritableFont.NO_BOLD);   
  17.     try{   
  18.        wwb = Workbook.createWorkbook(os);   
  19.        ws = wwb.createSheet("总部发票申请"0);   
  20.        font.setColour(Colour.WHITE);   
  21.        WritableCellFormat cellFormatContentt = new WritableCellFormat(font);   
  22.        font1.setColour(Colour.RED);   
  23.        WritableCellFormat cellFormatContentred = new WritableCellFormat(font1);   
  24.        ws.getSettings().setDefaultColumnWidth(18);   
  25.        //设置单元格格式  
  26.      cellFormat.setBackground(Colour.GRAY_25);   
  27.        cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);   
  28.        cellFormatContent.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);   
  29.        cellFormatContentt.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);   
  30.        int i = 0;   
  31.        for (InvoiceRequisitionSo so : result) {   
  32.              ws.addCell(new Label(0,i,so.getInvoice(),cellFormatContentred ));   
  33.               //合并单元格  
  34.          ws.mergeCells(1, i , 2, i );   
  35.               ws.addCell(new Label(1, i, so.getEno(), cellFormatContent));   
  36.               i++;   
  37.       }   
  38.     }catch (Exception e){   
  39.   
  40.     } finally{   
  41.             try {   
  42.                 wwb.write();   
  43.                 wwb.close();   
  44.                 os.close();   
  45.             } catch (Exception e) {   
  46.   
  47.             }   
  48.         }   
  49. }  

 

 

 

 

 

 

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelRead {
//判断excel版本
static Workbook openWorkbook(InputStream in,String filename,String fileFileName)throws IOException{
Workbook wb = null;
if(fileFileName.endsWith(".xlsx")){
wb = new XSSFWorkbook(in);//Excel 2007
} else {
wb = (Workbook) new HSSFWorkbook(in);//Excel 2003
}
return wb;
}

public static List<String[]> getExcelData(String fileName,String fileFileName) throws Exception {
InputStream in = new FileInputStream(fileName); //创建输入流
Workbook wb = openWorkbook(in, fileName,fileFileName);// 获取Excel文件对象
Sheet sheet = wb.getSheetAt(0);// 获取文件的指定工作表m 默认的第一个
List<String[]> list = new ArrayList<String[]>();
Row row = null;
Cell cell = null;
int totalRows = sheet.getPhysicalNumberOfRows(); // 总行数
int totalCells = sheet.getRow(0).getPhysicalNumberOfCells();//总列数
for (int i = 0; i < totalRows; i++) {
// 创建一个数组 用来存储每一列的值    
String[] str = new String[totalRows];
row = sheet.getRow(i);
for (int j = 0; j < totalCells; j++) {
cell = (Cell) sheet.getCellComment(j, i);
cell = row.getCell(j);
System.out.println(j+"DDDDDDDDDD");
//str[j] = cell.getRow(); 
}
// 把刚获取的列存入list
list.add(str);
}
for(int r=0; r<totalRows; r++) {
row = sheet.getRow(r);
System.out.print("第" + r + "行");
for(int c = 0; c < totalCells; c++){
cell = row.getCell(c);
String cellValue = "";
if(null != cell){
// 以下是判断数据的类型
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
cellValue = cell.getNumericCellValue() + "";
// 时间格式
// if(HSSFDateUtil.isCellDateFormatted(cell)){
// Date dd = cell.getDateCellValue();
// DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// cellValue = df.format(dd);
// }
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
cellValue = cell.getCellFormula() + "";
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
cellValue = "";
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
System.out.print("   "+cellValue+"\t");

}
}
System.out.println();
}
// 返回值集合
return list;
}

public static void main(String[] args) throws Exception{
String fileName = "C:/Users/w520-2/Desktop/供应商系统中英文对照.xlsx";
ExcelRead upload = new ExcelRead();
upload.getExcelData(fileName,".xls");
}
}

0 0
原创粉丝点击