⑴ 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();  
        }  
          
    }