java創立excel示例(jxl應用辦法)。本站提示廣大學習愛好者:(java創立excel示例(jxl應用辦法))文章只能為提供參考,不一定能成為您想要的結果。以下是java創立excel示例(jxl應用辦法)正文
應用該API非Windows操作體系也能夠經由過程純Java運用來處置Excel數據表。由於是應用 Java編寫的,所以我們在Web運用中可以經由過程JSP、Servlet來挪用API完成對Excel數據表的拜訪。
package com.yonyou.test;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.NumberFormats;
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 JxlTable {
private final static JxlTable jxlTable = new JxlTable();
public static JxlTable getInstance() {
return jxlTable;
}
public JxlTable() {
}
public boolean createTable(String header, String[] body, String filePath) {
boolean createFlag = true;
WritableWorkbook book;
try {
// 依據途徑生成excel文件
book = Workbook.createWorkbook(new File(filePath));
// 創立一個sheet名為"表格"
WritableSheet sheet = book.createSheet("表格", 0);
// 設置NO列寬度
sheet.setColumnView(1, 5);
// 去失落全部sheet中的網格線
sheet.getSettings().setShowGridLines(false);
Label tempLabel = null;
// 表頭輸入
String[] headerArr = header.split(",");
int headerLen = headerArr.length;
// 輪回寫入表頭內容
for (int i = 0; i < headerLen; i++) {
tempLabel = new Label(1 + i, 1, headerArr[i],
getHeaderCellStyle());
sheet.addCell(tempLabel);
}
// 表體輸入
int bodyLen = body.length;
// 輪回寫入表體內容
for (int j = 0; j < bodyLen; j++) {
String[] bodyTempArr = body[j].split(",");
for (int k = 0; k < bodyTempArr.length; k++) {
WritableCellFormat tempCellFormat = null;
tempCellFormat = getBodyCellStyle();
if (tempCellFormat != null) {
if (k == 0 || k == (bodyTempArr.length - 1)) {
tempCellFormat.setAlignment(Alignment.CENTRE);
}
}
tempLabel = new Label(1 + k, 2 + j, bodyTempArr[k],
tempCellFormat);
sheet.addCell(tempLabel);
}
}
book.write();
book.close();
} catch (IOException e) {
createFlag = false;
System.out.println("EXCEL創立掉敗!");
e.printStackTrace();
} catch (RowsExceededException e) {
createFlag = false;
System.out.println("EXCEL單位設置創立掉敗!");
e.printStackTrace();
} catch (WriteException e) {
createFlag = false;
System.out.println("EXCEL寫入掉敗!");
e.printStackTrace();
}
return createFlag;
}
public WritableCellFormat getHeaderCellStyle() {
WritableFont font = new WritableFont(WritableFont.createFont("宋體"), 10,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableCellFormat headerFormat = new WritableCellFormat(
NumberFormats.TEXT);
try {
// 添加字體設置
headerFormat.setFont(font);
// 設置單位格配景色:表頭為黃色
headerFormat.setBackground(Colour.YELLOW);
// 設置表頭表格邊框款式
// 全部表格線為粗線、黑色
headerFormat.setBorder(Border.ALL, BorderLineStyle.THICK,
Colour.BLACK);
// 表頭內容程度居中顯示
headerFormat.setAlignment(Alignment.CENTRE);
} catch (WriteException e) {
System.out.println("表頭單位格款式設置掉敗!");
}
return headerFormat;
}
public WritableCellFormat getBodyCellStyle() {
WritableFont font = new WritableFont(WritableFont.createFont("宋體"), 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableCellFormat bodyFormat = new WritableCellFormat(font);
try {
// 設置單位格配景色:表體為白色
bodyFormat.setBackground(Colour.WHITE);
// 設置表頭表格邊框款式
// 全部表格線為細線、黑色
bodyFormat
.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
} catch (WriteException e) {
System.out.println("表體單位格款式設置掉敗!");
}
return bodyFormat;
}
public static void main(String[] args) {
String header = "NO,姓名,性別,年紀";
String[] body = new String[4];
body[0] = "1,歐陽鋒,男,68";
body[1] = "2,黃藥師,男,67";
body[2] = "3,洪七公,男,70";
body[3] = "4,郭靖,男,32";
String filePath = "e:/test.xls";
JxlTable testJxl = JxlTable.getInstance();
boolean flag = testJxl.createTable(header, body, filePath);
if (flag) {
System.out.println("表格創立勝利!!");
}
}
}
分步講授:
讀取excel文件
要讀取excel文件起首應當樹立一個wordbook:
Workbook wb=Workbook.getWorkbook(File file);
Workbook wb=Workbook.getWorkbook(InputStream is);
然後便可以取得它的任務表:
Sheet[] sheets=wb.getSheets(); //取得一切任務表
Sheet sheet=wb.getSheet(0); //表現取得第一個任務表。
最初便可以取得對某任務表的某些單位格的值:
Cell cell=sheet.getCell(0,0); //取得第一列 第一行的數據。第一個參數為列
String value=cell.getContents(); //取得該單位格的字符串情勢的值
String type=cell.getType(); //取得該單位格的數據類型。
封閉workbook任務流:
當你完成對Excel電子表格數據的處置後,必定要應用close()辦法來封閉先前創立的對象,以釋放讀取數據表的 進程中所占用的內存空間,在讀取年夜量數據時顯得尤其主要。參考以下代碼片斷:
wb.close();
留意:只要完成對該excel的操作後才可以封閉,封閉後在挪用如Sheet s=wb.getSheet(0)會為null的。
經常使用的辦法引見:
Workbook類供給的辦法
1. int getNumberOfSheets()
取得任務薄(Workbook)中任務表(Sheet)的個數,示例:
jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));
int sheets = rwb.getNumberOfSheets();
2. Sheet[] getSheets()
前往任務薄(Workbook)中任務表(Sheet)對象數組,示例:
jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));
Sheet[] sheets = rwb.getSheets();
Sheet接口供給的辦法
1. String getName()
獲得Sheet的稱號,示例:
jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));
jxl.Sheet rs = rwb.getSheet(0);
String sheetName = rs.getName();
2. int getColumns()
獲得Sheet表中所包括的總列數,示例:
jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));
jxl.Sheet rs = rwb.getSheet(0);
int rsColumns = rs.getColumns();
3. Cell[] getColumn(int column)
獲得某一列的一切單位格,前往的是單位格對象數組,示例:
jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));
jxl.Sheet rs = rwb.getSheet(0);
Cell[] cell = rs.getColumn(0);
4. int getRows()
獲得Sheet表中所包括的總行數,示例:
jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File(sourcefile));
jxl.Sheet rs = rwb.getSheet(0);
int rsRows = rs.getRows();
5. Cell[] getRow(int row)