欧美特黄不卡,涩涩视频在线,成人国产精品一区二区网站,亚洲一区二区三区欧美

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

在Java中向Excel文件寫入內容

在Java中向Excel文件寫入內容

更新時間:2022-05-08 文章作者:未知 信息來源:網絡 閱讀次數:

可隨意轉載,但請注明出處及作者
SonyMusic
2003.05.15
==========================================================================
在Java中向Excel文件寫入內容


四、導出數據到Excel文件中
下面的例子,設置了數字、日期的格式,還有字體,顏色等。

File tempFile=new File("d:/temp/output.xls");
WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);

//一些臨時變量,用于寫到excel中
Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;

//預定義的一些字體和格式,同一個Excel中最好不要有太多格式
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat (headerFont);

WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat (titleFont);

WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat (detFont);

NumberFormat nf=new NumberFormat("0.00000");//用于Number的格式
WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);

DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的
WritableCellFormat dateFormat = new WritableCellFormat (detFont, df);

//剩下的事情,就是用上面的內容和格式創建一些單元格,再加到sheet中
l=new Label(0, 0, "用于測試的Excel文件", headerFormat);
sheet.addCell(l);

//add Title
int column=0;
l=new Label(column++, 2, "標題", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "日期", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "貨幣", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "價格", titleFormat);
sheet.addCell(l);

//add detail
int i=0;
column=0;
l=new Label(column++, i+3, "標題 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "CNY", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
sheet.addCell(n);

i++;
column=0;
l=new Label(column++, i+3, "標題 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "SGD", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
sheet.addCell(n);

//設置列的寬度
column=0;
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 10);
sheet.setColumnView(column++, 20);

workbook.write();
workbook.close();

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

主站蜘蛛池模板: 遂昌县| 明光市| 布拖县| 子长县| 保定市| 海伦市| 东兰县| 通江县| 嘉禾县| 商城县| 新晃| 乌兰浩特市| 颍上县| 拜城县| 溧阳市| 上犹县| 醴陵市| 瑞丽市| 安多县| 罗平县| 屏边| 扎鲁特旗| 英吉沙县| 寻甸| 突泉县| 阿克| 双辽市| 古浪县| 宝应县| 金阳县| 乌苏市| 萨嘎县| 法库县| 西藏| 遂昌县| 灌云县| 濮阳县| 龙岩市| 德格县| 通渭县| 弥勒县|