复制代码代码如下:
import java.io.BufferedInputStream;
java.io.File importieren;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
öffentliche Klasse FileOperator {
public static void main(String[] args) löst eine Ausnahme aus {
String path = "F://1.xls";
print(pfad);
}
public static void print(String path) löst eine IOException{ aus
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(new File(path)));
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell-Zelle = null;
HSSFSheet st = wb.getSheetAt(0);
for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
weitermachen;
}
for (short ColumnIndex = 0, size = row.getLastCellNum(); ColumnIndex <= size; ColumnIndex++) {
cell = row.getCell(columnIndex);
String-Wert = „“;
if (cell != null) {
// 注意:一定要设成这个,否则可能会出现乱码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
Fall HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
brechen;
Fall HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Datum date = cell.getDateCellValue();
if (Datum != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(Datum);
} anders {
value = "";
}
} anders {
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
brechen;
Fall HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} anders {
value = cell.getNumericCellValue() + "";
}
brechen;
Fall HSSFCell.CELL_TYPE_BLANK:
brechen;
Fall HSSFCell.CELL_TYPE_ERROR:
value = "";
brechen;
Fall HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
brechen;
Standard:
value = "";
}
System.out.println(rowIndex + "," + columnsIndex + ":"
+ Wert);
}
if (columnIndex == 0 && value.trim().equals("")) {
brechen;
}
}
}
}
}