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

这篇博客介绍了如何在Java中利用jxl库创建xls文件,并解决列宽自适应的问题。通过遍历数据并计算最长字符串长度,动态设置每列的最佳宽度,确保内容完整显示。同时,还展示了struts2Action中实现下载Excel文件的代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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++){
//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
String str = (list.get(i).get(j)+"").replace("null", "");
sheet.addCell(new Label(j,i,str));
int width = str.length()+getChineseNum(str);
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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值