`

Java导出Excel文档

    博客分类:
  • java
阅读更多
package tj;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.CellView;
import jxl.Workbook;
import jxl.write.Label;
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 ExcelUtil {
	public static void main(String[] args) {
		List<String> dataList=new ArrayList<String>();
		dataList.add("name,password,description");
		dataList.add("name1,pwd1,Hello发送的飞洒发送的发飞洒的范德萨范德萨发范德萨范德萨范德萨飞洒的飞洒发发第三方的萨芬的说法方法范德萨");
		dataList.add("name2,pwd2萨范德萨发范德萨范德萨范德萨飞洒的飞洒发发第三方的萨芬的说法方法范德萨范德萨范德萨范德萨,Hell");
		dataList.add("name3,pwd3,world");
		
		if(createExcel("d:\\a.xls", dataList)){
			System.out.println("创建成功!");
		}
	}
	private static boolean createExcel(String fileName,List<String> dataList){
		try {
			WritableWorkbook book=Workbook.createWorkbook(new File(fileName));
			WritableSheet sheet=book.createSheet("First page", 0);
			
			Label label=null;
			WritableFont font=new WritableFont(WritableFont.createFont("宋体"),12,WritableFont.BOLD);
			font.setColour(jxl.format.Colour.RED);//标题颜色
			WritableCellFormat fmt=new WritableCellFormat(font);
			fmt.setAlignment(jxl.format.Alignment.CENTRE);
			fmt.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			fmt.setWrap(true);
			
			String[] columnNames=dataList.get(0).split(",");
			for(int i=0;i<columnNames.length;i++){
				label=new Label(i,0,columnNames[i],fmt);
				sheet.addCell(label);
			}
			
			for(int i=1;i<dataList.size();i++){
				String[] rowValues=dataList.get(i).split(",");
				for(int j=0;j<columnNames.length;j++){
					label=new Label(j,i,rowValues[j]);
					sheet.addCell(label);
					
					CellView cv=new CellView();
					cv.setAutosize(true);
					sheet.setRowView(i, 400);
					sheet.setColumnView(j,cv);
				}
			}
			book.write();
			book.close();
			return true;
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
}

 

分享到:
评论
2 楼 xujijun11 2012-03-13  
怎么是excel,晕了
1 楼 xujijun11 2012-03-13  
很好,学习了

相关推荐

Global site tag (gtag.js) - Google Analytics