Dđọc nhiều sheet exel trong java

  • #1

Bài viết này hướng dẫn bạn cách đọc dữ liệu từ file Excel trong ứng dụng Java như thế nào. Chúng ta cần sử dụng thư viện jxl.jar, tải file về từ địa chỉ:
//congdongjava.com/thuvien/jxl-2.6.jar
sau đó add vào libary của chương trình
Dưới đây là demo, đọc 1 file xls bằng Java

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Tran Huy
*/
public class docfileexcel {
public static void main(String[] args) {
try {
File file = new File("C:\\demo.xls");
Workbook wb = Workbook.getWorkbook(file);
Sheet sheet = wb.getSheet(0);
int rows = sheet.getRows();
int cols = sheet.getColumns();
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
System.out.print(sheet.getCell(col, row).getContents()+" ");
}
System.out.println("\n");
}
} catch (Exception ex) {
Logger.getLogger(docfileexcel.class.getName()).log(Level.SEVERE, null, ex);
}

}
}

  • #2

Ðề: Đọc dữ liệu từ file excel trong Java

Đây là đọc, vậy còn ghi dữ liệu ra lại thì sao?
Cái nào giúp xử lý văn bản word nhỉ?

  • #3

Ðề: Đọc dữ liệu từ file excel trong Java

Xử lý văn bản word huy có làm một lần rồi, add API của thằng Ápache, nhưng nó không hổ trợ UTF-8 thì phải, không xử lý tiếng việt được, nên Huy bỏ ngang, chờ có time nghiên cứu lại nè ^_^

  • #4

Ðề: Đọc dữ liệu từ file excel trong Java

Có cái api giúp ghi file excel không cậu?

  • #6

Ðề: Đọc dữ liệu từ file excel trong Java

sao ko đọc được vậy ta, nó ko hiểu thằng Logger nửa chứ

  • #7

Ðề: Đọc dữ liệu từ file excel trong Java

các bác cho em hỏi? bây giờ e,m muốn đọc dữ liệu từng o một trong file excel thì làm thế nào ah. em đang viết một chương trình trắc nghiệm đinh lấy excel làm dữ liệu câu hỏi và đáp án.
vi dụ:
khi bắt đầu câu hỏi 1: cột đâu tiên là câu hỏi các cột sau là đáp án. tiếp theo các hàng tiếp theo là các câu hỏi tiếp
em mới tập tành nên còn non nớt mong các bác chỉ giáo ạh

  • #8

Ðề: Đọc dữ liệu từ file excel trong Java

không vó bác nào giúp em ah?

  • #9

Ðề: Đọc dữ liệu từ file excel trong Java

Các đơn giản nhất, sử dụng XML đi bạn, or sử dụng file đọc ghi có cấu trúc, là xong ^_^

  • #10

Ðề: Đọc dữ liệu từ file excel trong Java

các bác cho em hỏi? bây giờ e,m muốn đọc dữ liệu từng o một trong file excel thì làm thế nào ah. em đang viết một chương trình trắc nghiệm đinh lấy excel làm dữ liệu câu hỏi và đáp án.
vi dụ:
khi bắt đầu câu hỏi 1: cột đâu tiên là câu hỏi các cột sau là đáp án. tiếp theo các hàng tiếp theo là các câu hỏi tiếp
em mới tập tành nên còn non nớt mong các bác chỉ giáo ạh

Ngoài bộ thư viện mà Huy đã post như trên, bạn có thể dùng "APACHE POI" để thao tác với file Excel.
Vấn đề của bạn có thể giải quyết như sau(mình dùng "APACHE POI"), bạn tham khảo đoạn code mẫu dưới đây(để ý chỗ intRow):

PHP:

private void readingDataFromFile(String pathFile) { File fileExcel = new File(pathFile); POIFSFileSystem fsExcel = null; try { fsExcel = new POIFSFileSystem(new FileInputStream(fileExcel)); HSSFWorkbook wb = new HSSFWorkbook(fsExcel); HSSFSheet sheet = wb.getSheetAt(0); // first sheet System.out.println("TRUONGTEST: " + sheet.getLastRowNum()); for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) { Row row = rit.next(); int rowNum = row.getRowNum();//So dong hien tai(dùng de dem so dong) System.out.println("So dong hien tai: " + rowNum); if (rowNum > 1) { String masv = ""; String ho = ""; String ten = ""; int intRow = 0;//Bien dem cot cua tung dong(dây là cách giai quyet van de cua ban) for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) { Cell cell = cit.next(); switch (intRow) { case 0: cell.setCellType(Cell.CELL_TYPE_STRING); masv = cell.getStringCellValue(); intRow++; break; case 1: cell.setCellType(Cell.CELL_TYPE_STRING); ho = cell.getStringCellValue(); intRow++; break; case 2: cell.setCellType(Cell.CELL_TYPE_STRING); ten = cell.getStringCellValue(); intRow++; break; } System.out.println("Insert into SINHVIEN values(" + masv + "," + hoten + "," + gioitinh + "," + ngsinh + "," + noisinh + "," + hokhau + "," + diachi + ")"); } } } catch (Exception e) { e.printStackTrace(); } }

Chúc bạn thành công.

  • #11

Ðề: Đọc dữ liệu từ file excel trong Java

Hi! không biết admin có làm việc với nhưng file excell có cách encoding khác nhau chưa ví dụ như : UTF-16, UTF-8, windows-1252, Nếu làm rồi thì giúp mình nha. Mình đang bị vướng chỗ đó.
!!! Đọc file excel thôi.

  • #12

Ðề: Đọc dữ liệu từ file excel trong Java

bạn huy ơi! bạn có thể làm video hướng dân cách kết nối từ 1 file exel trong java cho mình và mọi người tham khảo với đc ko. Thank bạn nhiều

  • #13

Bài viết này hướng dẫn bạn cách đọc dữ liệu từ file Excel trong ứng dụng Java như thế nào. Chúng ta cần sử dụng thư viện jxl.jar, tải file về từ địa chỉ:
//congdongjava.com/thuvien/jxl-2.6.jar
sau đó add vào libary của chương trình
Dưới đây là demo, đọc 1 file xls bằng Java

Bài hướng dẫn thật là đơn giản. Bạn có thể hướng dẫn cách xuất ra file excell được không

  • #14

Bài hướng dẫn thật là đơn giản. Bạn có thể hướng dẫn cách xuất ra file excell được không

Bạn dùng thư viện poi ở đây //poi.apache.org/hwpf/index.html
Mình post cái class này lên đây bạn tự tìm hiểu nhé, khuya rồi nên không có thời gian hướng dẫn đầy đủ, dưới đây là cách export ra file excel từ ArrayList. method insertCustomer truyền dữ liệu vào excel từ ArrayList. truyền từ bảng thì làm tương tự.

Code:

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Method; import java.sql.*; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileFilter; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class ExportExcel { private Workbook wb; private Row row; private Sheet sheet; private File file; public void exportTable(String tableName){ } public void insert(ArrayList list,String sheetName,File file){ prepare(sheetName); insertValue(list); writeFile(file); openFile(file.getPath()); } private void prepare(String sheetName){ wb=new HSSFWorkbook(); int nos=wb.getNumberOfSheets(); for (int i = 0; i < nos; i++) { String sn=wb.getSheetName(i); if(sn.equalsIgnoreCase(sheetName)){ sheetName+="$1"; System.out.println(sheetName); } } //tạo worksheet sheet = wb.createSheet(sheetName); row = sheet.createRow((short)0); for (int i = 0; i < 4; i++) { Cell cell=row.createCell(i); cell.setCellValue("test"); } } private void writeFile(File file){ try (FileOutputStream fos = new FileOutputStream(file,true)) { try { wb.write(fos); } catch (IOException ex) { Validate.message="Có lỗi"; } }catch(Exception e){ Validate.message="Có lỗi"; } } public void insertRoom(){ } private void insertValue(ArrayList list){ // row và col là số hàng và cột for(int j=0;j<=row;j++){ Cell cell; row=sheet.createRow(j); // Tạo hàng for (int i = 0; i < col; i++) { value=list.get(i) cell=row.createCell(i); // Tạo ô cell.setCellValue(value); // Thêm giá trị vào ô } } } private void openFile(String path){ try { Desktop desktop = null; if (Desktop.isDesktopSupported()) { desktop = Desktop.getDesktop(); } desktop.open(new File(path)); } catch (IOException ioe) { System.out.println(ioe.toString()); } } public boolean choose(){ JFileChooser fc = new JFileChooser(); int save; fc.setDialogTitle("Export file"); save=fc.showSaveDialog(null); if(save==JFileChooser.APPROVE_OPTION){ file = fc.getSelectedFile(); return true; } return false; } }

  • #15

các bác cho em hỏi cái này nếu em muốn hiển thị toàn bộ thông tin lên form thì phải làm thế nào ạ. em gà cái này quá. vọc mãi mà không ra. thanks các bác

  • #16

Chào mọi người !
biết là đào mộ và vấn đề của mình có nhiều topic nói về rồi
nhưng mình làm theo những topic đó ko thu dc kết quả nên mới lên đây hỏi mọi người
mình cũng đọc file excel giống với cách trên nhưng bị lỗi ko tìm thấy file

Code:

public static void readData() { File fileExcel = new File("fishdata.xls"); try { Workbook wb = Workbook.getWorkbook(fileExcel); Sheet sheet = wb.getSheet(0); int rows = sheet.getRows(); for (int row = 2; row <= rows; row++) { int col = 0; //do something here } } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

còn đây là lỗi

Code:

java.io.FileNotFoundException: fishdata.xls (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at jxl.Workbook.getWorkbook(Workbook.java:213) at jxl.Workbook.getWorkbook(Workbook.java:198) at queryfish.db.db4o.Connection.readData(Connection.java:93) at queryfish.db.db4o.Connection.main(Connection.java:58)

trong file đính kèm là cấu trúc thư mục của mình
mong mọi người chỉ giúp mình lỗi sai
vì mình nghĩ đường dẫn chính xác rồi

  • 14.6 KB Lượt xem: 4

  • #17

Chào mọi người,
Mọi người cho mình hỏi tí, mình tạo 1 file excel 2013. Khi đọc file bằng java cả bằng POI và jxl đều báo lỗi: Unable to recognize OLE Stream. Lỗi như vậy là do file excel hay sao vậy mọi người.
Cảm ơn nhiều.

  • #18

Chào mọi người,
Mọi người cho mình hỏi tí, mình tạo 1 file excel 2013. Khi đọc file bằng java cả bằng POI và jxl đều báo lỗi: Unable to recognize OLE Stream. Lỗi như vậy là do file excel hay sao vậy mọi người.
Cảm ơn nhiều.

Có thể bạn bị nhầm, jxl khá cũ rồi hình như chỉ đọc ghi excel từ version 2003 trở xuống, còn POI đọc ghi được hết nhưng phải dùng đúng class ứng với từng phiên bản excel:
- Từ 2003 trở xuống dùng HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileExcel));
- Từ 2007 trở lên dùng XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileExcel));

  • #19

Xử lý file excel có một case rất hay ứng với trường hợp file excel lớn, có hàng trăm ngàn row

  • #20

Xử lý file excel có một case rất hay ứng với trường hợp file excel lớn, có hàng trăm ngàn row

SAX Parser

Chủ đề