java jxl导出excel小结
来源:互联网 发布:淘宝客返利网址 编辑:程序博客网 时间:2024/05/01 14:52
转载自:http://blog.csdn.net/xb12369/article/details/17507421
废话不多说,先上代码:
- package cn.com.jsoft.utils;
-
- import java.io.OutputStream;
- import java.io.UnsupportedEncodingException;
- import java.util.List;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.jsp.PageContext;
-
- import jxl.SheetSettings;
- import jxl.Workbook;
- import jxl.format.Alignment;
- import jxl.format.Colour;
- import jxl.format.VerticalAlignment;
- import jxl.write.Label;
- import jxl.write.WritableCellFormat;
- import jxl.write.WritableFont;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
-
-
-
-
-
-
- public class JxlExcelUtils {
-
-
-
-
-
-
-
-
- public static int exportToExcel(HttpServletResponse response, List<Map<String, Object>> objData, String sheetName,List<String> columns) {
- int flag = 0;
-
- WritableWorkbook wwb;
- try {
-
- OutputStream os = response.getOutputStream();
-
- wwb = Workbook.createWorkbook(os);
-
-
-
-
-
-
-
- WritableSheet ws = wwb.createSheet(sheetName, 0);
-
- SheetSettings ss = ws.getSettings();
- ss.setVerticalFreeze(1);
-
- WritableFont font1 =new WritableFont(WritableFont.createFont("微软雅黑"), 10 ,WritableFont.BOLD);
- WritableFont font2 =new WritableFont(WritableFont.createFont("微软雅黑"), 9 ,WritableFont.NO_BOLD);
- WritableCellFormat wcf = new WritableCellFormat(font1);
- WritableCellFormat wcf2 = new WritableCellFormat(font2);
- WritableCellFormat wcf3 = new WritableCellFormat(font2);
-
-
-
-
-
- wcf.setBackground(jxl.format.Colour.YELLOW);
- wcf.setAlignment(Alignment.CENTRE);
- wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
- wcf3.setAlignment(Alignment.CENTRE);
- wcf3.setVerticalAlignment(VerticalAlignment.CENTRE);
- wcf3.setBackground(Colour.LIGHT_ORANGE);
- wcf2.setAlignment(Alignment.CENTRE);
- wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);
-
-
-
-
-
- wcf.setAlignment(Alignment.CENTRE);
-
-
- if (columns != null && columns.size() > 0) {
-
-
- for (int i = 0; i < columns.size(); i++) {
-
-
-
-
-
-
-
-
-
-
- ws.addCell(new Label(i, 0, columns.get(i), wcf));
- }
-
-
- if (objData != null && objData.size() > 0) {
-
- for (int i = 0; i < objData.size(); i++) {
-
-
- Map<String, Object> map = (Map<String, Object>)objData.get(i);
-
-
- int j=0;
- for(Object o:map.keySet()){
-
- ws.addCell(new Label(j,i+1,String.valueOf(map.get(o))));
- j++;
- }
- }
- }else{
- flag = -1;
- }
-
-
- wwb.write();
-
-
- wwb.close();
-
-
- os.flush();
- os.close();
-
- os =null;
- }
- }catch (IllegalStateException e) {
- System.err.println(e.getMessage());
- }
- catch (Exception ex) {
- flag = 0;
- ex.printStackTrace();
- }
-
- return flag;
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void exportexcle(HttpServletResponse response,String filename,List<Map<String, Object>> listData,String sheetName,List<String> columns)
- {
-
- response.setContentType("application/vnd.ms-excel");
-
- try {
- response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1") + ".xls");
-
- exportToExcel(response, listData, sheetName, columns);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
-
-
- }
- }
调用的时候:- jxlUtils.exportexcle(response, filename, listData, sheetName, columns);
特别说明:response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1") + ".xls");
之前没有在后面加“.xls”的时候,在ie下导出后缀丢失。
目前只能导出xls,不能导出xlsx格式!
简单,明了。
0 0