Java实现Excel导入导出的工具类

来源:互联网 发布:农业 生长 大数据 编辑:程序博客网 时间:2024/06/06 19:10
import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletResponse;import jxl.Sheet;import jxl.Workbook;import jxl.format.Alignment;import jxl.format.Border;import jxl.format.BorderLineStyle;import jxl.format.VerticalAlignment;import jxl.read.biff.BiffException;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class ExcelUtil {public static <T> void exportExcel(String filename, String[] title, List<T> dataList,HttpServletResponse response) {try {OutputStream os = response.getOutputStream();// 取得输出流// 定义输出流,以便打开保存对话框response.reset();// 清空输出流response.setHeader("Content-disposition","attachment; filename=" + new String(filename.getBytes("GB2312"), "ISO8859-1"));// 设定输出文件头response.setContentType("application/msexcel");// 定义输出类型WritableWorkbook workbook = Workbook.createWorkbook(os);// 创建工作表WritableSheet sheet = workbook.createSheet("Sheet1", 0);//jxl.SheetSettings sheetset = sheet.getSettings();//sheetset.setProtected(false);WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);// 用于标题居中WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐wcf_center.setWrap(false); // 文字是否换行// 用于正文居左WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐wcf_left.setAlignment(Alignment.LEFT); // 文字水平对齐wcf_left.setWrap(false); // 文字是否换行for (int i = 0; i < title.length; i++) {sheet.addCell(new Label(i, 0, title[i], wcf_center));}// excel数据部分Field[] fields = null;int i = 1;for (Object obj : dataList) {fields = obj.getClass().getDeclaredFields();int j = 0;for (Field v : fields) {v.setAccessible(true);Object va = v.get(obj);if (va == null) {va = "";}sheet.addCell(new Label(j, i, va.toString(), wcf_left));j++;}i++;}// 将内容写入excel文件中workbook.write();workbook.close();} catch (Exception e) {e.printStackTrace();}}public static List<Object[]> importExcel(File fis){List<Object[]> list=new ArrayList<Object[]>();try {Workbook wb=Workbook.getWorkbook(fis);Sheet sheet=wb.getSheet(0);int rows=sheet.getRows();int columns=sheet.getColumns();Object [] rowsData;for(int i=1;i<rows;i++){rowsData=new Object[columns];for(int j=1;j<columns;j++){rowsData[j]="".equals(sheet.getCell(j, i).getContents())?null:sheet.getCell(j, i).getContents();}list.add(rowsData);}} catch (BiffException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return list;}}