利用JXL导出Excel文件
下面是导出Excel文件的代码,只是做一个简单的测试,没有格式字体的导出~
代码如下:
import java.io.FileOutputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import jxl.Workbook;import jxl.write.*;public class ExportTest {public static void main(String [] args){/** * 测试数据 */List<MyNews> list=new ArrayList<MyNews>();for(int i=0;i<5;i++){MyNews mynews=new MyNews();mynews.setTitle("aa"+i);mynews.setAuthor("bb"+i);mynews.setAcontent("cc"+i);list.add(mynews);}String targetfile="f:/excel.xls";String [] title={"title","author","acontent"};WritableWorkbook workbook;Label label;System.out.println("BEGIN....");try {OutputStream os=new FileOutputStream(targetfile);workbook=Workbook.createWorkbook(os);WritableSheet sheet=workbook.createSheet("MySheet", 1);//设置标题for(int i=0;i<title.length;i++){label=new Label(i,0,title[i]);sheet.addCell(label);}//设置单元格的值for(int i=0;i<list.size();i++){MyNews m=(MyNews)list.get(i);sheet.addCell(new Label(0,i+1,m.getTitle()));sheet.addCell(new Label(1,i+1,m.getAuthor()));sheet.addCell(new Label(2,i+1,m.getAcontent()));}//写入workbook.write();//关闭workbook.close();} catch (Exception e) {e.printStackTrace();}System.out.println("end....");}}
?
?
每天一点点希望对大家有利~