java獲取excel指定行內容
阿新 • • 發佈:2019-02-14
package HolidaySummaryReport;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//可適用.xls/ .xlsx
public class readExcel {
@SuppressWarnings("resource")
public static Workbook openWorkbook(String fileName)
throws IOException {
InputStream in = new FileInputStream(fileName); // 建立輸入流
Workbook wb = null;
if (fileName.toLowerCase().endsWith(".xlsx")) {//toLowerCase 區分大小寫
wb = new XSSFWorkbook(in);// Excel 2007
} else {
wb = (Workbook) new HSSFWorkbook(in);// Excel 2003
}
return wb;
}
public static void poloRead(String fileName, int sheetIndex) {
try {
Workbook wb = openWorkbook(fileName);
Sheet sheet = wb.getSheetAt(sheetIndex);
int colNumCount = sheet.getRow(0).getPhysicalNumberOfCells();// 有多少列
FormulaEvaluator evaluator = wb.getCreationHelper()
.createFormulaEvaluator();// 獲取表格公式後的值
for (int i = 0; i < 1; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);//行
Cell cell = row.getCell((short) j);//列
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");//輸出結果
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
System.out.print("------------------------");
}
for (int i = 10; i < 11; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell((short) j);
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
System.out.print("------------------------");
}
for (int i = 14; i < 15; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell((short) j);
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
System.out.print("------------------------");
}
for (int i = 15; i < 16; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell((short) j);
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
}
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readpolo(){
File path = new File("D:\\User");
File[] files = path.listFiles();// 列出該目錄下所有檔案和資料夾
Arrays.sort(files, new Comparator<File>() {// 按照檔案最後修改日期倒序排序
@Override
public int compare(File file1, File file2) {
return (int) (file2.lastModified() - file1.lastModified());
}
});
// 取出第一個(即最新修改的)檔案,列印檔名
}
static public void main(String[] args){
readpolo();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//可適用.xls/ .xlsx
public class readExcel {
@SuppressWarnings("resource")
public static Workbook openWorkbook(String fileName)
throws IOException {
InputStream in = new FileInputStream(fileName); // 建立輸入流
Workbook wb = null;
if (fileName.toLowerCase().endsWith(".xlsx")) {//toLowerCase 區分大小寫
wb = new XSSFWorkbook(in);// Excel 2007
} else {
wb = (Workbook) new HSSFWorkbook(in);// Excel 2003
}
return wb;
}
public static void poloRead(String fileName, int sheetIndex) {
try {
Workbook wb = openWorkbook(fileName);
Sheet sheet = wb.getSheetAt(sheetIndex);
int colNumCount = sheet.getRow(0).getPhysicalNumberOfCells();// 有多少列
FormulaEvaluator evaluator = wb.getCreationHelper()
.createFormulaEvaluator();// 獲取表格公式後的值
for (int i = 0; i < 1; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);//行
Cell cell = row.getCell((short) j);//列
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");//輸出結果
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
System.out.print("------------------------");
}
for (int i = 10; i < 11; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell((short) j);
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
System.out.print("------------------------");
}
for (int i = 14; i < 15; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell((short) j);
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
System.out.print("------------------------");
}
for (int i = 15; i < 16; i++) {
for (int j = 0; j < colNumCount; j++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell((short) j);
CellValue cellValue = evaluator.evaluate(cell);// 獲表格公式計算後的值
if (cellValue != null) {
switch (cellValue.getCellType()) {// 判斷值型別
case Cell.CELL_TYPE_STRING:
String Stringvalue = cell.getStringCellValue();
System.out.println(Stringvalue + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
Double Doublevalue = cellValue.getNumberValue();
System.out.println(Doublevalue + " ");
break;
}
}
}
}
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readpolo(){
File path = new File("D:\\User");
File[] files = path.listFiles();// 列出該目錄下所有檔案和資料夾
Arrays.sort(files, new Comparator<File>() {// 按照檔案最後修改日期倒序排序
@Override
public int compare(File file1, File file2) {
return (int) (file2.lastModified() - file1.lastModified());
}
});
// 取出第一個(即最新修改的)檔案,列印檔名
String file = files[0].getName();
//以上這幾行是在一個資料夾裡獲取最新報表,可不要
// System.out.println(file);String fileName = "D://User//" + file;
//String fileName="D://User//user.xlsx";//不要上面自動獲取資料夾裡最新報表就是自己指定
poloRead(fileName, 4);}
static public void main(String[] args){
readpolo();
}
}