T2 02/03/2020 2. 40 giờ sáng
Xin chào,
Cảm ơn câu hỏi của bạn.
Không có phương thức trực tiếp nào trong Spire để lấy tên cột. Nhưng bạn có thể tham khảo đoạn mã sau để đạt được nó.
Mã.
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }2
Nếu tôi hiểu sai ý của bạn, vui lòng cung cấp thông tin chi tiết để điều tra thêm.
Trân trọng,
Betsy
Nhóm hỗ trợ E-iceblue
Betsy. bài đăng của giang. 3099Đã tham gia. T3 06/09/2016 8. 30 giờ sáng
Trong bài đăng này, chúng tôi sẽ thảo luận về cách đọc dữ liệu trang tính excel dựa trên tên cột thay vì chỉ mục cột
Sử dụng các phụ thuộc maven bên dưới cho POI apache trong pom. xml của dự án maven của bạn. [hiện đang dùng java poi phiên bản 5. x, tất cả các phiên bản mới nhất có thể được tìm thấy ở đây]
org.apache.poi poi 5.0.0 org.apache.poi poi-ooxml 5.0.0
Chúng tôi sẽ sử dụng lớp
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }3, lớp này tự động phát hiện loại Sổ làm việc phù hợp [HSSFWorkbook hoặc XSSFWorkbook] tùy thuộc vào định dạng excel. xls hoặc. xlsx
Đặt tên file excel và sheet
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }
Giải trình -
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }4 sẽ được gọi từ bất kỳ đâu trong khuôn khổ của bạn nếu bạn muốn đọc hoặc ghi bất kỳ dữ liệu trang tính excel nào, Trước tiên, bạn phải gọi phương thức này bằng cách gửi đường dẫn tệp excel và tên trang tính trước khi thực hiện bất kỳ thao tác đọc/ghi nào
Bản đồ
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }5 sẽ giữ tất cả các tiêu đề cột trong trang tính đó [hàng đầu tiên của trang tính, tôi. e
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }6], điều này sẽ giúp người dùng đọc dữ liệu dựa trên tiêu đề cột thay vì gửi chỉ mục cột. [cung cấp tính linh hoạt để đọc dữ liệu]
Dòng 28 đến 31 & 37 đến 29 là tùy chọn nếu bạn chắc chắn sử dụng đúng đường dẫn tệp và tên trang tính
Bây giờ chúng ta có thể thêm các phương thức liên quan đến thao tác đọc hoặc ghi trong cùng một lớp ExcelUtil
Đọc dữ liệu excel
public String getCellData[int rownum, int colnum] throws Exception{ try{ cell = sh.getRow[rownum].getCell[colnum]; String CellData = null; switch [cell.getCellType[]]{ case STRING: CellData = cell.getStringCellValue[]; break; case NUMERIC: if [DateUtil.isCellDateFormatted[cell]] { CellData = String.valueOf[cell.getDateCellValue[]]; } else { CellData = String.valueOf[[long]cell.getNumericCellValue[]]; } break; case BOOLEAN: CellData = Boolean.toString[cell.getBooleanCellValue[]]; break; case BLANK: CellData = ""; break; } return CellData; }catch [Exception e]{ return""; } } public String getCellData[String columnName, int rownum] throws Exception { return getCellData[rownum, columns.get[columnName]]; }
Giải trình
Như bạn có thể thấy, chúng tôi có
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }0
Sử dụng getCellData[] đầu tiên, nếu bạn biết chính xác số hàng và cột của trang tính để tìm nạp dữ liệu
ô dành riêng cho số hàng & cột
Trường hợp có thể là
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }1,
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }2,
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }3, v.v. đây không là gì ngoài loại ô, loại dữ liệu mà ô có, dựa trên loại chúng tôi đang chuyển đổi dữ liệu thành chuỗi
Lưu ý – Khi loại ô là SỐ, getNumericCellValue trả về dữ liệu dưới dạng double i. e trả về 123456. 0 nếu ô có dữ liệu 123456, đó là lý do tại sao chúng tôi đang chuyển đổi loại ô thành
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }4 và sau đó sử dụng phương pháp
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }5 để xóa. 0 từ giá trị được tìm nạp
Sử dụng getCellData[] thứ 2, nếu bạn muốn tìm nạp dữ liệu dựa trên tên cột và số hàng
Lưu ý ở đây, ở đây chúng tôi đang sử dụng bản đồ cột để tìm nạp chỉ mục cột dựa trên tham số String columnName & gọi / chỉ sử dụng getCellData[] đầu tiên để tìm nạp dữ liệu trang tính excel
Hãy xem cách sử dụng phương pháp này
Chúng tôi có một bảng excel testData. xlsx trong thư mục dự án
Tôi đang sử dụng phương thức chính để đọc nội dung Trang tính 1, nhưng bạn có thể gọi từ bất kỳ vị trí nào trong khuôn khổ của mình
public static void main[String []args] throws Exception { ExcelUtil excel = new ExcelUtil[]; excel.setExcelFile["./testData.xlsx", "Sheet1"]; System.out.println[excel.getCellData["UserName", 1]]; System.out.println[excel.getCellData["Password", 1]]; System.out.println[excel.getCellData["Valid", 1]]; }
đầu ra
Quản trị viên
Quản trị viên123.
Có
nhận không. của dữ liệu hàng hiện tại
Mã dưới đây sẽ không tìm nạp. của dữ liệu hàng có trong mỗi trang tính
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }0
Mã trên này sẽ trả về không. hàng kể cả hàng thứ nhất
Ghi dữ liệu vào sheet excel
package excel; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.usermodel.*; public class ExcelUtil { private FileInputStream fis; private FileOutputStream fileOut; private Workbook wb; private Sheet sh; private Cell cell; private Row row; private CellStyle cellstyle; private Color mycolor; private String excelFilePath; private Map columns = new HashMap[]; public void setExcelFile[String ExcelPath, String SheetName] throws Exception { try { File f = new File[ExcelPath]; if [!f.exists[]] { f.createNewFile[]; System.out.println["File doesn't exist, so created!"]; } fis = new FileInputStream[ExcelPath]; wb = WorkbookFactory.create[fis]; sh = wb.getSheet[SheetName]; //sh = wb.getSheetAt[0]; //0 - index of 1st sheet if [sh == null] { sh = wb.createSheet[SheetName]; } this.excelFilePath = ExcelPath; //adding all the column header names to the map 'columns' sh.getRow[0].forEach[cell ->{ columns.put[cell.getStringCellValue[], cell.getColumnIndex[]]; }]; } catch [Exception e] { System.out.println[e.getMessage[]]; } } }1
Phương pháp trên này cần 3 tham số, ô i. e số hàng & cột và văn bản bạn muốn ghi vào trang tính excel bằng cách sử dụng ô. setCellValue[]