⑴ java开发怎么导入导出excel里面信息内容
publicclassExcelExport{
	/**
	*默认每个sheet页最多显示的行数
	*/
	privatestaticfinalintsheet_rows=50000;
	/**
	*导出Excel文件
	*
	*@paramtitleList
	*表头信息
	*@paramdataList
	*表格数据
	*@paramfileName
	*导出文件完整名称demo.xls
	*@paramrequest
	*@paramresponse
	*@throwsIOException
	*/
	(List<String>titleList,
			List<List<String>>dataList,StringfileName,
			HttpServletRequestrequest,HttpServletResponseresponse)
			throwsIOException{
		HSSFWorkbookworkBook=exportDataToExcel(titleList,dataList);
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("UTF-8");
		fileName=encodeFilename(fileName,request);
		response.setHeader("Content-disposition","attachment;filename="+fileName);
		workBook.write(response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	}
	(Stringfilename,HttpServletRequestrequest){
		Stringagent=request.getHeader("USER-AGENT");
		try{
			if((agent!=null)&&(0<=agent.indexOf("Firefox"))){
				returnMimeUtility.encodeText(filename,"UTF-8","B");
			}elseif((agent!=null)&&(0<=agent.indexOf("Chrome"))){
				
				returnfilename=newString(filename.getBytes(),"ISO8859-1");
			}
			else{
				if(agent!=null){
					StringnewFileName=URLEncoder.encode(filename,"UTF-8");
					newFileName=StringUtils.replace(newFileName,"+","%20");
					if(newFileName.length()>150){
						newFileName=newString(filename.getBytes("GB2312"),
								"ISO8859-1");
						newFileName=StringUtils.replace(newFileName,"",
								"%20");
					}
					returnnewFileName;
				}
			}
		}catch(Exceptionex){
			returnfilename;
		}
		returnfilename;
	}
	(List<String>titleList,List<List<String>>dataList){
		/*1.创建一个Excel文件*/
		HSSFWorkbookworkbook=newHSSFWorkbook();
		/*2.创建Excel的一个Sheet*/
		HSSFSheetsheet=workbook.createSheet();
		/*3.创建表头冻结*/
		sheet.createFreezePane(0,1);
		/*4.设置列宽*/
		for(inti=0;i<titleList.size();i++){
			sheet.setColumnWidth(i,5000);
		}
		/*5.表头字体*/
		HSSFFontheadfont=workbook.createFont();
		headfont.setFontName("宋体");
		headfont.setFontHeightInPoints((short)12);//字体大小
		headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
		/*6.表头样式*/
		HSSFCellStyleheadstyle=workbook.createCellStyle();
		headstyle.setFont(headfont);
		headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
		//设置背景色为蓝色
		headstyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
		headstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		/*7.普通单元格字体*/
		HSSFFontfont=workbook.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short)12);
		/*8.普通单元格样式*/
		HSSFCellStylestyle=workbook.createCellStyle();
		style.setFont(font);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
		/*9.拼装表头*/
		Iterator<String>titleRowIterator=titleList.iterator();
		intcolumnIndex=0;
		HSSFRowrow=sheet.createRow(0);
		while(titleRowIterator.hasNext()){
			StringcellValue=titleRowIterator.next();
			HSSFCellcell=row.createCell(columnIndex);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue(cellValue);
			cell.setCellStyle(headstyle);
			columnIndex++;
			cell=null;
		}
		/*10.组织表数据*/
		Iterator<List<String>>rowIterator=dataList.iterator();
		introwIndex=1;
		while(rowIterator.hasNext()){
			List<String>columnList=rowIterator.next();
			row=sheet.createRow(rowIndex);
			Iterator<String>columnIterator=columnList.iterator();
			columnIndex=0;
			while(columnIterator.hasNext()){
				StringcellValue=columnIterator.next();
				HSSFCellcell=row.createCell(columnIndex);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(cellValue);
				cell.setCellStyle(style);
				cell=null;
				columnIndex++;
			}
			row=null;
			rowIndex++;
		}
		returnworkbook;
	}
	
	/**
	*重载导出数据到Excel中
	*@paramtitleList表头
	*@paramdataList表数据
	*@paramamount每个sheet页显示行数
	*@return
	*/
	(List<String>titleList,List<List<String>>dataList,intamount){
		/*1.创建一个Excel文件*/
		HSSFWorkbookworkbook=newHSSFWorkbook();
		
		//校验传入的参数
		if(titleList==null){
			titleList=newArrayList<String>();
		}
		//无数据直接返回
		if(dataList==null||dataList.size()==0){
			returnworkbook;
		}
		//传入数据不正确,按照默认条数显示
		if(amount>65535||amount<=0){
			amount=sheet_rows;
		}
		
		//获取sheet页的数量
		introw_num=0;
		inty=dataList.size()%amount;
		if(y==0){
			row_num=dataList.size()/amount;
		}else{
			row_num=dataList.size()/amount+1;
		}
		
		/*表头字体*/
		HSSFFontheadfont=workbook.createFont();
		headfont.setFontName("宋体");
		headfont.setFontHeightInPoints((short)12);//字体大小
		headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
		/*表头样式*/
		HSSFCellStyleheadstyle=workbook.createCellStyle();
		headstyle.setFont(headfont);
		headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
		//设置背景色为蓝色
		headstyle.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
		headstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		
		/*普通单元格字体*/
		HSSFFontfont=workbook.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short)12);
		/*普通单元格样式*/
		HSSFCellStylestyle=workbook.createCellStyle();
		style.setFont(font);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
		
		//循环写入每个sheet页
		for(inti=0;i<row_num;i++){
			/*创建Excel的一个Sheet*/
			HSSFSheetsheet=workbook.createSheet();
			/*创建表头冻结*/
			sheet.createFreezePane(0,1);
			/*设置列宽*/
			for(intt=0;t<titleList.size();t++){
				sheet.setColumnWidth(t,5000);
			}
			/*拼装表头*/
			Iterator<String>titleRowIterator=titleList.iterator();
			intcolumnIndex=0;
			HSSFRowrow=sheet.createRow(0);
			while(titleRowIterator.hasNext()){
				StringcellValue=titleRowIterator.next();
				HSSFCellcell=row.createCell(columnIndex);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(cellValue);
				cell.setCellStyle(headstyle);
				columnIndex++;
				cell=null;
			}
			/*组织表数据*/
			introwIndex=1;
			for(intj=amount*i;(j<amount*(i+1)&&j<dataList.size());j++){
				List<String>columnList=dataList.get(j);
				row=sheet.createRow(rowIndex);
				Iterator<String>columnIterator=columnList.iterator();
				columnIndex=0;
				while(columnIterator.hasNext()){
					StringcellValue=columnIterator.next();
					HSSFCellcell=row.createCell(columnIndex);
					cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					cell.setCellValue(cellValue);
					cell.setCellStyle(style);
					cell=null;
					columnIndex++;
				}
				row=null;
				rowIndex++;
			}
		}
		returnworkbook;
	}
	
	/**
	*重载导出Excel功能,新增一项amount每个sheet导出记录行数
	*@paramtitleList
	*@paramdataList
	*@paramfileName
	*@paramamount行数如果小于等于0或大于65535则按照默认显示
	*@paramrequest
	*@paramresponse
	*@throwsIOException
	*/
	(List<String>titleList,
			List<List<String>>dataList,StringfileName,intamount,
			HttpServletRequestrequest,HttpServletResponseresponse)
			throwsIOException{
		HSSFWorkbookworkBook=exportDataToExcel(titleList,dataList,amount);
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("UTF-8");
		fileName=encodeFilename(fileName,request);
		response.setHeader("Content-disposition","attachment;filename="+fileName);
		workBook.write(response.getOutputStream());
		response.getOutputStream().flush();
		response.getOutputStream().close();
	}
	
}
说明:main方法里的第一个参数为要导出的表头信息,
第二个参数为数据信息,
第三个参数设置文件名,设置好以后直接调用就可以导出Excle了;
导入就是把文件存到服务器,可用Mogodb等数据库存储,以下是读取已存根据存储的文件id解析Excle数据的大致过程:
POIFSFileSystemfs=null;
if(!StringUtils.isBlank(fileId)){
fs=newPOIFSFileSystem(newByteArrayInputStream(
//传入对应的文件对象
MongoFileUtil.readFile(fileId)));
}
//构造XSSFWorkbook对象,filePath传入文件路径
HSSFWorkbookxwb=newHSSFWorkbook(fs);
//读取第一个sheet
HSSFSheetsheet=xwb.getSheetAt(0);
for(inti=sheet.getFirstRowNum()+1;i<sheet.getPhysicalNumberOfRows();i++){
//解析每行的数据
HSSFRowsingleRow=sheet.getRow(i);
//解析每个单元格数据
singleRow.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
	StringcellValue=singleRow.getCell(0).getStringCellValue();
}
⑵ java 导出 Excel 中文乱码
HSSFCellcsCell=row.createCell((short)1);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置袭cell编码解决中文高位字节截断
csCell.setCellValue("中文测试");//设置中文
⑶ java生成excel命名为什么不能为中文
没有太多原因,POI就是如此;再说导出的EXCEL文件名的中文乱码问题, 导出时代码如下:
.....
this.getResponse().reset();
this.getResponse().setContentType("application/msexcel");
this.getResponse().setHeader("Content-Disposition", "inline;filename=中文名.xls");
try {
em.getExcelMutliIO(this.getResponse().getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这个时候导出去时,文件名会为乱码,解决的办法如下,在你的代码增加下列函数:
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = Character.toString(c).getBytes("utf-8");}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString(); 
}
然后在导出时,对文件名引用该函数,代码如下:
this.getResponse().setHeader("Content-Disposition", "inline;filename=" +toUtf8String("中文文件名.xls"));
⑷ 如何用Java导出数据存到excel里面
java中jxl导出数据到excel的例子 
import jxl.*;   
import jxl.write.*;   
import java.io.*;   
import java.io.File.*;   
import java.util.*;   
public class excel   
{   
public static void main(String[] args)    
{   
String targetfile = "c:/out.xls";//输出的excel文件名   
String worksheet = "List";//输出的excel文件工作表名   
String[] title = {"ID","NAME","DESCRIB"};//excel工作表的标题   
WritableWorkbook workbook;   
try 
{   
//创建可写入的工作薄,运行生成的文件在tomcat/bin下   
//workbook = Workbook.createWorkbook(new File("output.xls"));    
System.out.println("begin");   
OutputStream os=new FileOutputStream(targetfile);    
workbook=Workbook.createWorkbook(os);    
WritableSheet sheet = workbook.createSheet(worksheet, 0); //添加第一个工作表   
//WritableSheet sheet1 = workbook.createSheet("MySheet1", 1); //可添加第二个工作   
/* 
jxl.write.Label label = new jxl.write.Label(0, 2, "A label record"); //put a label in cell A3, Label(column,row) 
sheet.addCell(label);   
*/ 
jxl.write.Label label;   
for (int i=0; i<title.length; i++)   
{   
//Label(列号,行号 ,内容 )   
label = new jxl.write.Label(i, 0, title[i]); //put the title in row1    
sheet.addCell(label);    
}   
//下列添加的对字体等的设置均调试通过,可作参考用   
//添加数字   
jxl.write.Number number = new jxl.write.Number(3, 4, 3.14159); //put the number 3.14159 in cell D5   
sheet.addCell(number);   
//添加带有字型Formatting的对象    
jxl.write.WritableFont wf = new jxl.write.WritableFont(WritableFont.TIMES,10,WritableFont.BOLD,true);    
jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);    
jxl.write.Label labelCF = new jxl.write.Label(4,4,"文本",wcfF);    
sheet.addCell(labelCF);    
//添加带有字体颜色,带背景颜色 Formatting的对象    
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);    
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);    
wcfFC.setBackground(jxl.format.Colour.BLUE);   
jxl.write.Label labelCFC = new jxl.write.Label(1,5,"带颜色",wcfFC);    
sheet.addCell(labelCFC);    
//添加带有formatting的Number对象    
jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");    
jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(nf);    
jxl.write.Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);    
sheet.addCell(labelNF);    
//3.添加Boolean对象    
jxl.write.Boolean labelB = new jxl.write.Boolean(0,2,false);    
sheet.addCell(labelB);    
//4.添加DateTime对象    
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());    
sheet.addCell(labelDT);    
//添加带有formatting的DateFormat对象    
jxl.write.DateFormat df = new jxl.write.DateFormat("ddMMyyyyhh:mm:ss");    
jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);    
jxl.write.DateTime labelDTF = new jxl.write.DateTime(1,3,new java.util.Date(),wcfDF);    
sheet.addCell(labelDTF);    
//和宾单元格   
//sheet.mergeCells(int col1,int row1,int col2,int row2);//左上角到右下角   
sheet.mergeCells(4,5,8,10);//左上角到右下角   
wfc = new jxl.write.WritableFont(WritableFont.ARIAL,40,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);    
jxl.write.WritableCellFormat wchB = new jxl.write.WritableCellFormat(wfc);    
wchB.setAlignment(jxl.format.Alignment.CENTRE);   
labelCFC = new jxl.write.Label(4,5,"单元合并",wchB);    
sheet.addCell(labelCFC); //   
//设置边框   
jxl.write.WritableCellFormat wcsB = new jxl.write.WritableCellFormat();    
wcsB.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THICK);   
labelCFC = new jxl.write.Label(0,6,"边框设置",wcsB);    
sheet.addCell(labelCFC);    
workbook.write();    
workbook.close();   
}catch(Exception e)    
{    
e.printStackTrace();    
}    
System.out.println("end");   
Runtime r=Runtime.getRuntime();    
Process p=null;    
//String cmd[]={"notepad","exec.java"};    
String cmd[]={"C:\\Program Files\\Microsoft Office\\Office\\EXCEL.EXE","out.xls"};    
try{    
p=r.exec(cmd);    
}    
catch(Exception e){    
System.out.println("error executing: "+cmd[0]);    
}   
}   
}
⑸ java打开excel乱码
HSSFWorkbook workbook = new HSSFWorkbook();//创建EXCEL文件 
HSSFSheet   sheet= workbook.createSheet(sheetName);       //创建工作表 
这样在用英文名作为工作表名是没问题的,但如果sheetName是中文字符,就会出现乱码,解决的方法如下代码: 
HSSFSheet   sheet= workbook.createSheet(); 
workbook.setSheetName(0, sheetName,(short)1); //这里(short)1是解决中文乱码的关键;而第一个参数是工作表的索引号。 
没有太多原因,POI就是如此;再说导出的EXCEL文件名的中文乱码问题, 导出时代码如下: 
..... 
this.getResponse().reset();
this.getResponse().setContentType("application/msexcel");
this.getResponse().setHeader("Content-Disposition", "inline;filename=中文名.xls");
try {
em.getExcelMutliIO(this.getResponse().getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 
这个时候导出去时,文件名会为乱码,解决的办法如下,在你的代码增加下列函数: 
public static String toUtf8String (String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = Character.toString(c).getBytes("utf-8");}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j =  0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();  
} 
然后在导出时,对文件名引用该函数,代码如下: 
this.getResponse().setHeader("Content-Disposition", "inline;filename=" +toUtf8String("中文文件名.xls"));
⑹ 求高手解决Java做导出excel时出现的中文乱码,包括文件名好内容中的中文 String headerStr = "13621
编码格式统统用UTF-8
⑺ java怎么导出excel表格
通过这个例子,演示以下如何用java生成excel文件:
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
publicclass CreateCells
{
publicstaticvoid main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);//建立新行
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);//建立新cell
cell.setCellValue(1);//设置cell的整数类型的值
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值
row.createCell((short)2).setCellValue("test");//设置cell字符类型的值
row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值
HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式
HSSFCell dCell =row.createCell((short)4);
dCell.setCellValue(new Date());//设置cell为日期类型的值
dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式
HSSFCell csCell =row.createCell((short)5);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断
csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串
row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
⑻ 如何在java中导入导出excel
使旅尘用谈镇做含衡 I/O流
http://www.cnblogs.com/wuxinrui/archive/2011/03/20/1989326.html
⑼ java poi导出excel
用spire.xls.jar也可以导出excel,代码更简单
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class InsertArray {
public static void main(String[] args) {
//创建Workbook对象
Workbook wb = new Workbook();
//获取第一张工作表
Worksheet sheet = wb.getWorksheets().get(0);
//定义一维数据
String[] oneDimensionalArray = new String[]{"苹果", "梨子", "葡萄", "香蕉"};
//将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入
sheet.insertArray(oneDimensionalArray, 1, 1, true);
//定义二维数组
String[][] twoDimensionalArray = new String[][]{
{"姓名", "年龄", "性别", "学历"},
{"小张", "25", "男", "本科"},
{"小王", "24", "男", "本科"},
{"小李", "26", "女", "本科"}
};
//从指定单元格开始写入二维数组到工作表
sheet.insertArray(twoDimensionalArray, 1, 3);
//保存文档
wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);
}
}
⑽ java端导出Excel表格。
可以使用poi来实现导出execl表格或者通过io流实现导出execl表格,但是poi相对来说更方便
实例如下:
    try{  
            HSSFWorkbook workbook = new HSSFWorkbook();                     // 创建工作簿对象  
            HSSFSheet sheet = workbook.createSheet(title);                  // 创建工作表  
              
            // 产生表格标题行  
            HSSFRow rowm = sheet.createRow(0);  
            HSSFCell cellTiltle = rowm.createCell(0);  
              
            //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面  - 可扩展】  
            HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象  
            HSSFCellStyle style = this.getStyle(workbook);                  //单元格样式对象  
              
            sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));    
            cellTiltle.setCellStyle(columnTopStyle);  
            cellTiltle.setCellValue(title);  
              
            // 定义所需列数  
            int columnNum = rowName.length;  
            HSSFRow rowRowName = sheet.createRow(2);                // 在索引2的位置创建行(最顶端的行开始的第二行)  
              
            // 将列头设置到sheet的单元格中  
            for(int n=0;n<columnNum;n++){  
                HSSFCell  cellRowName = rowRowName.createCell(n);               //创建列头对应个数的单元格  
                cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);             //设置列头单元格的数据类型  
                HSSFRichTextString text = new HSSFRichTextString(rowName[n]);  
                cellRowName.setCellValue(text);                                 //设置列头单元格的值  
                cellRowName.setCellStyle(columnTopStyle);                       //设置列头单元格样式  
            }  
              
            //将查询出的数据设置到sheet对应的单元格中  
            for(int i=0;i<dataList.size();i++){  
                  
                Object[] obj = dataList.get(i);//遍历每个对象  
                HSSFRow row = sheet.createRow(i+3);//创建所需的行数  
                  
                for(int j=0; j<obj.length; j++){  
                    HSSFCell  cell = null;   //设置单元格的数据类型  
                    if(j == 0){  
                        cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);  
                        cell.setCellValue(i+1);   
                    }else{  
                        cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);  
                        if(!"".equals(obj[j]) && obj[j] != null){  
                            cell.setCellValue(obj[j].toString());                       //设置单元格的值  
                        }  
                    }  
                    cell.setCellStyle(style);                                   //设置单元格样式  
                }  
            }  
            //让列宽随着导出的列长自动适应  
            for (int colNum = 0; colNum < columnNum; colNum++) {  
                int columnWidth = sheet.getColumnWidth(colNum) / 256;  
                for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {  
                    HSSFRow currentRow;  
                    //当前行未被使用过  
                    if (sheet.getRow(rowNum) == null) {  
                        currentRow = sheet.createRow(rowNum);  
                    } else {  
                        currentRow = sheet.getRow(rowNum);  
                    }  
                    if (currentRow.getCell(colNum) != null) {  
                        HSSFCell currentCell = currentRow.getCell(colNum);  
                        if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {  
                            int length = currentCell.getStringCellValue().getBytes().length;  
                            if (columnWidth < length) {  
                                columnWidth = length;  
                            }  
                        }  
                    }  
                }  
                if(colNum == 0){  
                    sheet.setColumnWidth(colNum, (columnWidth-2) * 256);  
                }else{  
                    sheet.setColumnWidth(colNum, (columnWidth+4) * 256);  
                }  
            }  
              
            if(workbook !=null){  
                try  
                {  
                    String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";  
                    String headStr = "attachment; filename=\"" + fileName + "\"";  
                    response = getResponse();  
                    response.setContentType("APPLICATION/OCTET-STREAM");  
                    response.setHeader("Content-Disposition", headStr);  
                    OutputStream out = response.getOutputStream();  
                    workbook.write(out);  
                }  
                catch (IOException e)  
                {  
                    e.printStackTrace();  
                }  
            }  
  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
          
    }