永发信息网

java怎么读取excel文件

答案:2  悬赏:30  手机版
解决时间 2021-02-15 07:52
  • 提问者网友:溺爱和你
  • 2021-02-14 21:13
java怎么读取excel文件
最佳答案
  • 五星知识达人网友:走死在岁月里
  • 2021-02-14 22:21
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 ExcelHandle {
public static void main(String[] args) throws Exception {
File file = new File("sss.xls");
String[][] result = getData(file, 1);
int rowLength = result.length;
for(int i=0;i for(int j=0;j System.out.print(result[i][j]+"\t\t");
}
System.out.println();
}

}

public static String[][] getData(File file, int ignoreRows)throws FileNotFoundException, IOException {
List result = new ArrayList();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打开HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行为标题,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要设成这个,否则可能会出现乱码cell.setEncoding(HSSFCell.ENCODING_UTF_16); 3.2已经自动Unicode处理了
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y": "N");
break;
default:
value = "";
}
}
if (columnIndex == 0 && value.trim().equals("")) {
break;
}
values[columnIndex] = rightTrim(value);
hasValue = true;
}

if (hasValue) {
result.add(values);
}
}
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
}


public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i >= 0; i--) {
if (str.charAt(i) != 0x20) {
break;
}
length--;
}
return str.substring(0, length);
}
}

给你个例子代码,应该能帮到你!
全部回答
  • 1楼网友:冷風如刀
  • 2021-02-14 23:24
package com.sidi.oa.util; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import org.apache.poi.hssf.usermodel.hssfcell; 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 excelreader { private hssfworkbook wb = null;// book [includes sheet] private hssfsheet sheet = null; private hssfrow row = null; private int sheetnum = 0; // 第sheetnum个工作表 private int rownum = 0; private fileinputstream fis = null; private file file = null; public excelreader() { } public excelreader(file file) { this.file = file; } public void setrownum(int rownum) { this.rownum = rownum; } public void setsheetnum(int sheetnum) { this.sheetnum = sheetnum; } public void setfile(file file) { this.file = file; } public void open() throws ioexception { fis = new fileinputstream(file); wb = new hssfworkbook(new poifsfilesystem(fis)); fis.close(); } public int getsheetcount() { int sheetcount = -1; sheetcount = wb.getnumberofsheets(); return sheetcount; } public int getrowcount() { if (wb == null) system.out.println("=============>workbook为空"); hssfsheet sheet = wb.getsheetat(this.sheetnum); int rowcount = -1; rowcount = sheet.getlastrownum(); return rowcount; } public int getrowcount(int sheetnum) { hssfsheet sheet = wb.getsheetat(sheetnum); int rowcount = -1; rowcount = sheet.getlastrownum(); return rowcount; } public string[] readexcelline(int linenum) { return readexcelline(this.sheetnum, linenum); } public string[] readexcelline(int sheetnum, int linenum) { if (sheetnum < 0 || linenum < 0) return null; string[] strexcelline = null; try { sheet = wb.getsheetat(sheetnum); row = sheet.getrow(linenum); int cellcount = row.getlastcellnum(); strexcelline = new string[cellcount + 1]; for (int i = 0; i <= cellcount; i++) { strexcelline[i] = readstringexcelcell(linenum, i); } } catch (exception e) { e.printstacktrace(); } return strexcelline; } public string readstringexcelcell(int cellnum) { return readstringexcelcell(this.rownum, cellnum); } public string readstringexcelcell(int rownum, int cellnum) { return readstringexcelcell(this.sheetnum, rownum, cellnum); } public string readstringexcelcell(int sheetnum, int rownum, int cellnum) { if (sheetnum < 0 || rownum < 0) return ""; string strexcelcell = ""; try { sheet = wb.getsheetat(sheetnum); row = sheet.getrow(rownum); if (row.getcell((short) cellnum) != null) { // add this condition // judge switch (row.getcell((short) cellnum).getcelltype()) { case hssfcell.cell_type_formula: strexcelcell = "formula "; break; case hssfcell.cell_type_numeric: { strexcelcell = string.valueof(row.getcell((short) cellnum) .getnumericcellvalue()); } break; case hssfcell.cell_type_string: strexcelcell = row.getcell((short) cellnum) .getstringcellvalue(); break; case hssfcell.cell_type_blank: strexcelcell = ""; break; default: strexcelcell = ""; break; } } } catch (exception e) { e.printstacktrace(); } return strexcelcell; } //主函数用于测试 public static void main(string args[]) { file file = new file("c:\\importperson.xls"); excelreader readexcel = new excelreader(file); try { readexcel.open(); } catch (ioexception e) { e.printstacktrace(); } readexcel.setsheetnum(0); // 设置读取索引为0的工作表 // 总行数 int count = readexcel.getrowcount(); for (int i = 0; i <= count; i++) { string[] rows = readexcel.readexcelline(i); for (int j = 0; j < rows.length; j++) { system.out.print(rows[j] + " "); } system.out.print("\n"); } } } 注:这是我在网上找的,这几天也在整这个问题,经测试这段代码完全没问题,版权归原作者所有。怎么样,给分吧,哪里不行,可以问我 呵呵
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯