[英]What should I import to be able to use the following code correctly?
嗨,我正在尝试将 Excel 文件读入我的 android 应用程序。 我尝试使用此代码:
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
int rows; // No of rows
rows = sheet.getPhysicalNumberOfRows();
int cols = 0; // No of columns
int tmp = 0;
// This trick ensures that we get the data properly even if it doesn't start from first few rows
for(int i = 0; i < 10 || i < rows; i++) {
row = sheet.getRow(i);
if(row != null) {
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if(tmp > cols) cols = tmp;
}
}
for(int r = 0; r < rows; r++) {
row = sheet.getRow(r);
if(row != null) {
for(int c = 0; c < cols; c++) {
cell = row.getCell((short)c);
if(cell != null) {
// Your code here
}
}
}
}
} catch(Exception ioe) {
ioe.printStackTrace();
}
我想知道我是否应该导入任何东西或任何库?
如果你使用 Eclipse 作为你的 IDE(我假设你是因为这是 Android 相关的)你可以按 ctrl + shift + O 并且它会自动导入你需要的任何东西,假设你的构建路径中包含了库。 但是,听起来您没有 Apache POI 库。 首先,您需要下载库。 下载它们后,您需要使用以下方法将它们添加到构建路径中:(再次假设您使用 Eclipse 作为 IDE)
或者,您可以将库添加到您的项目文件夹之一(通常是“lib”文件夹),在 Eclipse 中右键单击它并选择“添加到构建路径”。
现在应该将它添加到您的构建路径中。 再次尝试按 ctrl + shift + O 并为您制作必要的导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.