java實現Excel資料匯入匯出,javaexcel匯入匯出
使用的是jxl,具體jar可以前往官網下載,匯出資料採取的是假資料,樣本很簡單。
Model類:
public class Model {private String name;private String score;public Model(String name, String score) {super();this.name = name;this.score = score;}public Model() {super();// TODO Auto-generated constructor stub}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getScore() {return score;}public void setScore(String score) {this.score = score;}@Overridepublic String toString() {return "Model [name=" + name + ", score=" + score + "]";}}
讀取代碼:
@Testpublic void read() throws BiffException, IOException{List<Model> list = new ArrayList<Model>();Workbook book = Workbook.getWorkbook(new File("test.xls"));int columns = 0 ;int rows = 0;String name = "";String score ="";Sheet[] sheet = book.getSheets();Model m = null;for (int i = 0; i < sheet.length; i++) { rows = sheet[i].getRows();//多少資料 System.out.print("第"+(i+1)+"頁資料:"+rows); columns = sheet[i].getColumns(); //多少欄位 System.out.println("\t第"+(i+1)+"分頁欄位:"+columns); for (int j = 0; j < rows; j++) { m = new Model(); for (int c = 0; c < columns; c++) { Cell cell = sheet[i].getCell(c,j); if(c == 1) { name = cell.getContents(); m.setName(name); } else { score = cell.getContents(); m.setScore(score); }} list.add(m); }}for (Model m1 : list) {System.out.println(m1.toString());}}
匯出代碼:
@Testpublic void write() throws IOException, RowsExceededException, WriteException{List<Model> list = new ArrayList<Model>();Model m1 = new Model("test1","22");Model m2 = new Model("test2","33");Model m3 = new Model("test3","44");Model m4 = new Model("test4","55");Model m5 = new Model("test5","66");list.add(m1);list.add(m2);list.add(m3);list.add(m4);list.add(m5);WritableWorkbook book = Workbook.createWorkbook(new File("test4.xls"));//建立一個Excel檔案WritableSheet sheet = book.createSheet("Sheet", 0);//建立一個Sheet頁Label label = null;int s = 2;//控制匯出列數for (int i = 0; i < list.size(); i++) {for (int j = 0; j <s; j++) {if(j==0){label = new Label(j, i, list.get(i).getName());sheet.addCell(label);}else{label = new Label(j, i, list.get(i).getScore());sheet.addCell(label);}}}book.write();book.close();}
怎用java匯入Excel資料到資料庫?
public static void main(String args[]) throws BiffException, IOException, WriteException{
//1 從Excel檔案讀取資料表
//Java Excel API既可以從本地檔案系統的一個檔案(.xls),也可以從輸入資料流中讀取Excel資料表。
//讀取Excel資料表的第一步是建立Workbook(術語:工作薄),下面的程式碼片段舉例說明了應該如何操作:
//(完整代碼見ExcelReading.java)
try
{
//構建Workbook對象, 唯讀Workbook對象
//直接從本地檔案建立Workbook
//從輸入資料流建立Workbook
InputStream is = new FileInputStream("D:/user.xls");
jxl.Workbook rwb = Workbook.getWorkbook(is);
//一旦建立了Workbook,我們就可以通過它來訪問Excel Sheet(術語:工作表)。參考下面的程式碼片段:
//擷取第一張Sheet表
Sheet rs = (Sheet) rwb.getSheet(0);
//我們既可能通過Sheet的名稱來訪問它,也可以通過下標來訪問它。如果通過下標來訪問的話,
//要注意的一點是下標從0開始,就像數組一樣。
//一旦得到了Sheet,我們就可以通過它來訪問Excel Cell(術語:儲存格)。參考下面的程式碼片段:
//擷取第一行,第一列的值
Cell c00 = ((jxl.Sheet) rs).getCell(0, 0);
String strc00 = c00.getContents();
//擷取第一行,第二列的值
Cell c10 = ((jxl.Sheet) rs).getCell(1, 0);
String strc10 = c10.getContents();
//擷取第二行,第二列的值
Cell c11 = ((jxl.Sheet) rs).getCell(1, 1);
String strc11 = c11.getContents();
System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());
System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());
System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());
//如果僅僅是取得Cell的值,我們可以方便地通過getConten......餘下全文>>
怎在java程式中匯入excel資料
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReadExcel2 {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("src/test.xls");
String[][] data = getData(file, 0);
printStringArray(data);
}
public static void printStringArray(String[][] data){
for(int i =0; i< data.length; i++){
for(int j=0; j< data[i].length; j++){
System.out.print(data[i][j]+"\t");
}
System.out.print("\n");
}
}
/**
*
* 讀取Excel的內容,第一維數組儲存的是一行中格列的值,二維數組儲存的是多少個行
* @param file 讀取資料的源Excel
* @param ignoreRows 讀取資料忽略的行數,比喻行頭不需要讀入 忽略的行數為1
* @return 讀出的Excel中資料的內容
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file, int ignoreRows)
throws......餘下全文>>