//废话不说,直接上代码:
需要引用的jar包,下载地址:http://download.youkuaiyun.com/detail/lujianwen/7963381 , 也可以自已去官网下载
jar包:dom4j-1.6.1.jar;poi-3.10.1-20140818.jar;poi-ooxml-3.10.1-20140818.jar;poi-ooxml-schemas-3.10.1-20140818.jar;xbean.jar
import java.io.FileInputStream;
import java.io.InputStream;
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.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static void main(String[] args) throws Exception {
System.out.println("begin");
String excelPath = "Excel文件路徑";
String password = "Excel文件密碼";
String prefix = excelPath.substring(excelPath.lastIndexOf(".") + 1);
Workbook workbook;
InputStream inp = new FileInputStream(excelPath);
if (prefix.toUpperCase().equals("XLS")) {
org.apache.poi.hssf.record.crypto.Biff8EncryptionKey
.setCurrentUserPassword(password);
workbook = WorkbookFactory.create(inp);
inp.close();
} else {
POIFSFileSystem pfs = new POIFSFileSystem(inp);
inp.close();
EncryptionInfo encInfo = new EncryptionInfo(pfs);
Decryptor decryptor = Decryptor.getInstance(encInfo);
decryptor.verifyPassword(password);
workbook = new XSSFWorkbook(decryptor.getDataStream(pfs));
}
Sheet sheet = workbook.getSheetAt(0);
int startRowNum = sheet.getFirstRowNum();
int endRowNum = sheet.getLastRowNum();
for (int rowNum = startRowNum; rowNum <= endRowNum; rowNum++) {
Row row = sheet.getRow(rowNum);
if (row == null)
continue;
int startCellNum = row.getFirstCellNum();
int endCellNum = row.getLastCellNum();
for (int cellNum = startCellNum; cellNum < endCellNum; cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell == null)
continue;
int type = cell.getCellType();
switch (type) {
case Cell.CELL_TYPE_NUMERIC:// 数值、日期类型
double d = cell.getNumericCellValue();
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 日期类型
Date date = cell.getDateCellValue();
System.out.print(" "
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(date) + " ");
} else {// 数值类型
System.out.print(" " + d + " ");
}
break;
case Cell.CELL_TYPE_BLANK:// 空白单元格
System.out.print(" null ");
break;
case Cell.CELL_TYPE_STRING:// 字符类型
System.out.print(" " + cell.getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_BOOLEAN:// 布尔类型
System.out.println(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.err.println("非法字符");// 非法字符;
break;
default:
System.err.println("error");// 未知类型
break;
}
}
System.out.println();
}
System.out.println("end");
}
}
本文提供了一段Java代码示例,演示如何使用POI库读取带有密码加密的Excel文件。所需的POI及相关依赖jar包可以在指定链接下载。通过这段代码,开发者可以了解如何处理加密的Excel文件。
1433

被折叠的 条评论
为什么被折叠?



