java解析excel文件的方法
作者:wanghjbuf 时间:2023-05-13 15:53:44
建立工程前需要导入POI包。POI相关jar包下载地址:http://poi.apache.org/download.html
1.解析.xlsx后缀名的的EXCEL文件:
package com.shuai.hello;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static void main(String[] args) throws IOException {
//File file = new File("C:/Users.xlsx");
InputStream stream = new FileInputStream("C:/Users.xlsx");
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(stream);
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
int rowstart = xssfSheet.getFirstRowNum();
int rowEnd = xssfSheet.getLastRowNum();
for(int i=rowstart;i<=rowEnd;i++)
{
XSSFRow row = xssfSheet.getRow(i);
if(null == row) continue;
int cellStart = row.getFirstCellNum();
int cellEnd = row.getLastCellNum();
for(int k=cellStart;k<=cellEnd;k++)
{
XSSFCell cell = row.getCell(k);
if(null==cell) continue;
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
System.out.print(cell.getNumericCellValue()
+ "\t");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue()
+ "\t");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue()
+ "\t");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + "\t");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
}
System.out.print("\n");
}
}
}
/*String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length());
InputStream stream = new FileInputStream(filePath);
Workbook wb = null;
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(stream);
} else if (fileType.equals("xlsx")) {
wb = new XSSFWorkbook(stream);
} else {
System.out.println("您输入的excel格式不正确");
}*/
2.解析后缀为.xls的EXCEL文件:
package com.shuai.hello;
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 ReadXls {
public static void main(String[] args) throws IOException, IOException {
File file = new File("C:/Users/dengta/Desktop/ok1.xls");
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
int rowstart = hssfSheet.getFirstRowNum();
int rowEnd = hssfSheet.getLastRowNum();
for(int i=rowstart;i<=rowEnd;i++)
{
HSSFRow row = hssfSheet.getRow(i);
if(null == row) continue;
int cellStart = row.getFirstCellNum();
int cellEnd = row.getLastCellNum();
for(int k=cellStart;k<=cellEnd;k++)
{
HSSFCell cell = row.getCell(k);
if(null==cell) continue;
//System.out.print("" + k + " ");
//System.out.print("type:"+cell.getCellType());
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
System.out.print(cell.getNumericCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + " ");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
}
System.out.print("\n");
}
}
}
来源:http://blog.csdn.net/wanghjbuf/article/details/54809134?locationNum=4&fps=1
标签:java,解析,excel
0
投稿
猜你喜欢
java 开发中网络编程之IP、URL详解及实例代码
2023-08-06 10:26:29
Android 自定义view实现进度条加载效果实例代码
2022-12-21 05:33:48
Java Struts图片上传至指定文件夹并显示图片功能
2023-03-15 10:48:51
Java实现的Base64加密算法示例
2023-10-29 00:37:08
自定义AlertDialog去除黑色背景的解决方法
2023-05-18 09:56:17
mybatis if标签判断不生效的解决方法
2023-11-28 12:30:49
java开发MVC三层架构上再加一层Manager层原理详解
2023-06-14 06:10:51
Android使用CardView作为RecyclerView的Item并实现拖拽和左滑删除
2022-03-20 00:33:28
C#中登录窗体和欢迎窗体关闭方法分析
2023-03-23 03:58:25
Java中过滤器 (Filter) 和 拦截器 (Interceptor)的使用
2023-07-07 00:20:28
Android ListView仿微信聊天界面
2023-10-15 04:59:46
Springboot如何添加server.servlet.context-path相关使用
2023-03-14 10:35:03
eclipse怎么引入spring boot项目插件的方法
2021-07-23 20:38:54
详解Mybatis中的select方法
2023-06-10 05:44:49
C++求四个正整数最大公约数的方法
2023-02-04 12:09:19
分享安装Android Studio3.6的经验教训
2021-12-24 07:39:29
android实现横屏的代码及思路
2023-06-25 09:35:40
Java编程复用类代码详解
2021-09-13 10:06:34
详解MyBatis-Plus Wrapper条件构造器查询大全
2023-09-05 08:55:52
java在linux本地执行shell命令的实现方法
2023-11-14 02:31:33