import org.apache.poi.hssf.usermodel.*;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.extractor.XSSFExcelExtractor;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;import java.util.Iterator;import java.util.List;
public class Main {
//////////////////////////////////////
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"sandbox_test.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
////
// Theo Active (sheet đang được thao tác):
Worksheet excelWorksheet = (Worksheet)this.excelWorkbook.ActiveSheet;
Worksheet sheet = (Worksheet)xlApp.Worksheets[1];
sheet.Select(Type.Missing);
or
sheet.Activate();
//////////////
import org.apache.poi.ss.util.CellReference;
public class Excel {
public static void convertInttoString(){
CellReference cellReference = new CellReference(0,0); System.out.println(cellReference.formatAsString());
String regex = "R([0-9]+)C([0-9]+)";
}
public static void main(String[] args) {
convertInttoString(); }
}
///////////
private static final String FILE_NAME = "D:/test.xlsx"; static String handleStringCell(Cell cell) {
String contents = cell.getRichStringCellValue().getString(); return contents; }
static String handleNonStringCell( Cell cell, DataFormatter formatter) {
String contents = ""; CellType type = cell.getCellTypeEnum(); if (type == CellType.FORMULA) {
type = cell.getCachedFormulaResultTypeEnum(); }
if (type == CellType.NUMERIC) {
CellStyle cs = cell.getCellStyle();
if (cs != null && cs.getDataFormatString() != null) {
contents = formatter.formatRawCellContents(
cell.getNumericCellValue(), cs.getDataFormat(), cs.getDataFormatString()); }
}else{
// No supported styling applies to this cell contents = ((XSSFCell)cell).getRawValue(); }
return contents; }
public static void main(String[] args) {
////////////////////////////////////////// DataFormatter dataFormatter = new DataFormatter();//////////////////////////////////////////
try {
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME)); Workbook workbook = new XSSFWorkbook(excelFile); Sheet datatypeSheet = workbook.getSheetAt(0); Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next(); Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next(); currentCell.getAddress(); // HSSFName name = (HSSFName) workbook.getNameAt(0);
////////////////////////////////////////// String cellStringValue = dataFormatter.formatCellValue(currentCell); System.out.println(cellStringValue); XSSFExcelExtractor a = new XSSFExcelExtractor((XSSFWorkbook) workbook); a.getText();
System.out.println("===================="+dataFormatter.formatCellValue(currentCell)+"============================"); // Rows and cells
// Is it a formula one? if(currentCell.getCellTypeEnum() == CellType.FORMULA) {
if (currentCell.getCellFormula()!="") {
String contents = currentCell.getCellFormula(); System.out.println(contents); } else {
if (currentCell.getCachedFormulaResultTypeEnum() == CellType.STRING) {
handleStringCell(currentCell); } else {
handleNonStringCell(currentCell, dataFormatter); }
}
} else if(currentCell.getCellTypeEnum() == CellType.STRING) {
handleStringCell( currentCell); } else {
handleNonStringCell(currentCell, dataFormatter); }
//////////////////////////////////////////
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); }
}
}
//////////pom
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version></dependency>
0 comments:
Post a Comment