import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import java.lang.reflect.Field;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.CellFormat;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class CreateXL {
/**
* @param args
*/
public static void main(String[] args) {
CreateXL C = new CreateXL();
System.out.println(C.excelPage());
}
/***************************************************************************
* @param fileName EXCEL文件名称
* @param listTitle EXCEL文件第一行列标题集合
* @param listContent EXCEL文件正文数据集合
* @return
*/
public final static String exportExcel(String fileName,String[] Title, List<List> listContent) {
String result="系统提示:Excel文件导出成功!";
try {
WritableWorkbook book = Workbook.createWorkbook(new File(fileName));
WritableSheet sheet = book.createSheet("第一页",0);
for(int i=0;i<Title.length;i++){
sheet.addCell(new Label(i,0,Title[i]));
}
int i=1;
for(List a:listContent){
for(int x=0;x<a.size();x++){
sheet.addCell(new Label(x,i,(String)a.get(x)));
}
i++;
}
book.write();
book.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
return result;
}
/**
* 导出excel
* @return
*/
public String excelPage(){
CreateXL excel=new CreateXL();
String str="";
List<List> li=new ArrayList();
List l=new ArrayList();
for(int i =0 ; i<3;i++){
l.add(""+i+"");
}
li.add(l);
li.add(l);
li.add(l);
li.add(l);
System.out.println(li);
String[] Title={"名称","省名称","城市名称"};
return excel.exportExcel("D:\\DB\\客户资料信息.xls",Title, li);
}
}
java 生成Excel 的方法,布布扣,bubuko.com
java 生成Excel 的方法