import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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;
/**
* 類說明
*
* @author nmj
* @email [email protected]
* @date 2017年5月17日 新建
*/
public class dc {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// 設置列寬WritableCellFormat
// 設置單元格自適應
WritableFont fontTitle = new WritableFont(
WritableFont.createFont("宋體"), 12, WritableFont.NO_BOLD);
// fontTitle.setColour(jxl.format.Colour.RED);
WritableCellFormat formatTitle = new WritableCellFormat(fontTitle);
formatTitle.setWrap(true);
CellView c = new CellView();
c.setSize(25 * 256);
c.setFormat(formatTitle);
// 首先創建一個用於保存excel表格的文件
File file = new File("C:\\Users\\nmj\\Desktop\\test.xls");
// 創建一個文件輸出流,用於寫出表格到本地文件夾
OutputStream out = new FileOutputStream(file);
// 創建一個可讀寫入的工作薄(相當於創建了一個excel表格。抽象類,不能用new來創建)
WritableWorkbook workbook = Workbook.createWorkbook(out);
// 創建一個新頁(一個excel文件可以包含多頁哦。參數一:本頁名稱;參數二:頁數,第一頁爲0)
WritableSheet sheet = workbook.createSheet("第一頁", 0);
// 將第一行的樣式設置爲c
sheet.setColumnView(0, c);
sheet.setColumnView(1, c);
sheet.setColumnView(2, c);
// 添加單元格至本頁中
Label cell1 = new Label(0, 0, "一行一列"); // 1行1列
sheet.addCell(cell1);
Label cell2 = new Label(1, 0, "一行二列"); // 1行2列
sheet.addCell(cell2);
Label cell3 = new Label(2, 0, "一行三列"); // 1行3列
sheet.addCell(cell3);
Label cell4 = new Label(1, 1, "二行二列"); // 2行2列
sheet.addCell(cell4);
// 寫入Excel工作表
workbook.write();
// 關閉Excel工作表,同時也會關閉IO流,勿忘。
workbook.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相關jar下載:http://download.csdn.net/download/nmj2015/9844443java使用jxl導出excel並單元格自適應
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.