jxl导出xls文件以及解决列宽的问题

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
* 创建xls文件并输出
* @author gyl
*
*/
public class SimpleExcel {

public static void createExcel(OutputStream os,ArrayList<ArrayList<Object>> list)
throws WriteException,IOException{
//创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet("Sheet1",0);
        if(list!=null&&list.size()>0){
        int columnBestWidth[]=new  int[list.get(0).size()];    //保存最佳列宽数据的数组
        for(int i=0;i<list.size();i++){
            for(int j=0;j<list.get(i).size();j++){
            //创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
            sheet.addCell(new Label(j,i,list.get(i).get(j).toString()));
            int width = list.get(i).get(j).toString().length()+getChineseNum(list.get(i).get(j).toString());
            if(columnBestWidth[j]<width){
            columnBestWidth[j]=width+2;///求取到目前为止的最佳列宽
            }
            }
            }
        for(int i=0;i<columnBestWidth.length;i++){    ///设置每列宽
                sheet.setColumnView(i, columnBestWidth[i]);
            }
        }
        workbook.write();
        workbook.close();
        os.close();
}

public static int getChineseNum(String context){    ///统计context中是汉字的个数
        int lenOfChinese=0;
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");    //汉字的Unicode编码范围
        Matcher m = p.matcher(context);
        while(m.find()){
            lenOfChinese++;
        }
        return lenOfChinese;
    }

}


strust2Action部分:

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.ewash.common.util.SimpleExcel;

/**
* xls下载
* @param list
*/
public void downloadExcel(ArrayList<ArrayList<Object>> list,String fileName){
try {
OutputStream os = getResponse().getOutputStream();
fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
getResponse().setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
getResponse().setContentType("application/msexcel");
SimpleExcel.createExcel(os, list);
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 获取 application 对象.
* @return ServletContext
*/
public ServletContext getApplication() {
return ServletActionContext.getServletContext();
}

/**
* 获取请求对象.
* @return HttpServletRequest
*/
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}

/**
* 获取请求对象.
* @return HttpServletRequest
*/
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}

/**
* 获得传递参数
* @param str
* @return String
*/
public String getParameter(String str) {
return getRequest().getParameter(str);
}

你可能感兴趣的:(java)