问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

Poi如何去读取excel文件

发布网友 发布时间:2022-04-26 04:52

我来回答

2个回答

热心网友 时间:2022-05-11 21:59

直接全部在action里面写的,这个就不多说了,直接上代码:

public String executeExcel() throws Exception{ String realPath = ServletActionContext.getServletContext().getRealPath("/fileupload");
System.out.println(fileFileName);
String filePath = "";
if(this.file!=null){
File saveFile = new File(new File(realPath),this.fileFileName);
filePath = realPath+"\\"+this.fileFileName;
System.out.println(filePath);
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs(); }
FileUtils.copyFile(file, saveFile); }
this.exlToDB(filePath);
ActionContext.getContext().put("message","导入成功");
return "success"; } //读取excel2007,并把数据插入数据库
public void exlToDB(String filePath){ boolean flag = true;
AllKpi akpi = new AllKpi(); try { // 文件流指向excel文件
FileInputStream fin=new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(fin);// 创建工作薄
XSSFSheet sheet = workbook.getSheetAt(0);// 得到工作表
XSSFRow row = null;// 对应excel的行
XSSFCell cell = null;// 对应excel的列
int totalRow = sheet.getLastRowNum();// 得到excel的总记录条数
System.out.println(totalRow); // 以下的字段一一对应数据库表的字段
float idd = 0.0f;
String id = "";
String Name = "";
String DEPT_NAME = "";
String Weight = "";
<span></span>String ALGORITHM = "";
String text = " ";
//String sql = "insert into DSP_TJ_KPI values(DSP_TJ_KPI_SEQ.nextval,?,?,?,'无',?)";
for (int i = 1; i <= totalRow; i++) {
row = sheet.getRow(i);
//System.out.println(row.getCell(0).toString());
if(row.getCell(0) != null && !"".equals(row.getCell(0)) && row.getCell(1) != null && !"".equals(row.getCell(1)) && row.getCell(2) != null && !"".equals(row.getCell(2)) && row.getCell(3) != null && !"".equals(row.getCell(3))){
cell = row.getCell((short) 0);
Name = cell.toString();
System.out.println(Name);
cell = row.getCell((short) 1);
Weight = cell.toString();
System.out.println(Weight);
cell = row.getCell((short) 2);
DEPT_NAME = cell.toString();
System.out.println(DEPT_NAME);
cell = row.getCell((short) 3);
ALGORITHM = cell.toString();
System.out.println(ALGORITHM);
akpi.setAllkpiName(Name);
akpi.setAllkpiDeptName(DEPT_NAME);
akpi.setAllkpiWeight(Weight);
akpi.setAlgorithm(ALGORITHM);
akpi.setText(text);
allKpiService.addAllKpi(akpi); //以下注释代码为连接jdbc测试代码块
/*pst = con.prepareStatement(sql);
//pst.setString(1, student_id);
pst.setString
(1, DEPT_NAME);
pst.setString
(2, Name);
pst.setString
(3, Weight);
<span></span>pst.setString(4, ALGORITHM);
pst.execute();*/
System.out.println("preparestatement successful"); } }
/*pst.close();
con.close();*/
fin.close(); } catch (FileNotFoundException e)
{
flag = false;
e.printStackTrace();
}
catch (IOException ex)
{
flag = false;
ex.printStackTrace();
}

热心网友 时间:2022-05-11 23:17

Poi如何去读取excel文件

主要是通过poi去去读取文件的每一行的。下载poi的jar文件是poi-bin-3.8-beta2-20110408.zip 那下边来分享一下代码吧:
package com.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;
public class TestExcel {
//记录类的输出信息
static Log log = LogFactory.getLog(TestExcel.class);
//获取Excel文档的路径
public static String filePath = "D://excel.xls";
public static void main(String[] args) {
try {
// 创建对Excel工作簿文件
//HSSFWorkbook wookbook= new HSSFWorkbook(new FileInputStream(filePath));
// 在Excel文档中,第一张工作表的缺省索引是0,
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
// HSSFSheet sheet = wookbook.getSheet("Sheet1");
Workbook book = null;
try {
book = new XSSFWorkbook(filePath);
} catch (Exception ex) {
book = new HSSFWorkbook(new FileInputStream(filePath));
}

Sheet sheet = book.getSheet("Sheet1");
//获取到Excel文件中的所有行数
int rows = sheet.getPhysicalNumberOfRows();
//遍历行
for (int i = 0; i < rows; i++) {
// 读取左上端单元格
Row row = sheet.getRow(i);
// 行不为空
if (row != null) {
//获取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells();
String value = "";
//遍历列
for (int j = 0; j < cells; j++) {
//获取到列的值
Cell cell = row.getCell(j);
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
break;
case Cell.CELL_TYPE_NUMERIC:
value += cell.getNumericCellValue() + ",";
break;
case Cell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + ",";
break;
default:
value += "0";
break;
}
}
}
// 将数据插入到mysql数据库中
String[] val = value.split(",");
for(int j=0;j<val.length;j++){
//每一行列数中的值遍历出来
System.out.println(val[j]);
}

}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?0?2
?0?2
?0?2
?0?2
?0?2

热心网友 时间:2022-05-11 21:59

直接全部在action里面写的,这个就不多说了,直接上代码:

public String executeExcel() throws Exception{ String realPath = ServletActionContext.getServletContext().getRealPath("/fileupload");
System.out.println(fileFileName);
String filePath = "";
if(this.file!=null){
File saveFile = new File(new File(realPath),this.fileFileName);
filePath = realPath+"\\"+this.fileFileName;
System.out.println(filePath);
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs(); }
FileUtils.copyFile(file, saveFile); }
this.exlToDB(filePath);
ActionContext.getContext().put("message","导入成功");
return "success"; } //读取excel2007,并把数据插入数据库
public void exlToDB(String filePath){ boolean flag = true;
AllKpi akpi = new AllKpi(); try { // 文件流指向excel文件
FileInputStream fin=new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(fin);// 创建工作薄
XSSFSheet sheet = workbook.getSheetAt(0);// 得到工作表
XSSFRow row = null;// 对应excel的行
XSSFCell cell = null;// 对应excel的列
int totalRow = sheet.getLastRowNum();// 得到excel的总记录条数
System.out.println(totalRow); // 以下的字段一一对应数据库表的字段
float idd = 0.0f;
String id = "";
String Name = "";
String DEPT_NAME = "";
String Weight = "";
<span></span>String ALGORITHM = "";
String text = " ";
//String sql = "insert into DSP_TJ_KPI values(DSP_TJ_KPI_SEQ.nextval,?,?,?,'无',?)";
for (int i = 1; i <= totalRow; i++) {
row = sheet.getRow(i);
//System.out.println(row.getCell(0).toString());
if(row.getCell(0) != null && !"".equals(row.getCell(0)) && row.getCell(1) != null && !"".equals(row.getCell(1)) && row.getCell(2) != null && !"".equals(row.getCell(2)) && row.getCell(3) != null && !"".equals(row.getCell(3))){
cell = row.getCell((short) 0);
Name = cell.toString();
System.out.println(Name);
cell = row.getCell((short) 1);
Weight = cell.toString();
System.out.println(Weight);
cell = row.getCell((short) 2);
DEPT_NAME = cell.toString();
System.out.println(DEPT_NAME);
cell = row.getCell((short) 3);
ALGORITHM = cell.toString();
System.out.println(ALGORITHM);
akpi.setAllkpiName(Name);
akpi.setAllkpiDeptName(DEPT_NAME);
akpi.setAllkpiWeight(Weight);
akpi.setAlgorithm(ALGORITHM);
akpi.setText(text);
allKpiService.addAllKpi(akpi); //以下注释代码为连接jdbc测试代码块
/*pst = con.prepareStatement(sql);
//pst.setString(1, student_id);
pst.setString
(1, DEPT_NAME);
pst.setString
(2, Name);
pst.setString
(3, Weight);
<span></span>pst.setString(4, ALGORITHM);
pst.execute();*/
System.out.println("preparestatement successful"); } }
/*pst.close();
con.close();*/
fin.close(); } catch (FileNotFoundException e)
{
flag = false;
e.printStackTrace();
}
catch (IOException ex)
{
flag = false;
ex.printStackTrace();
}

热心网友 时间:2022-05-11 23:17

Poi如何去读取excel文件

主要是通过poi去去读取文件的每一行的。下载poi的jar文件是poi-bin-3.8-beta2-20110408.zip 那下边来分享一下代码吧:
package com.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;
public class TestExcel {
//记录类的输出信息
static Log log = LogFactory.getLog(TestExcel.class);
//获取Excel文档的路径
public static String filePath = "D://excel.xls";
public static void main(String[] args) {
try {
// 创建对Excel工作簿文件
//HSSFWorkbook wookbook= new HSSFWorkbook(new FileInputStream(filePath));
// 在Excel文档中,第一张工作表的缺省索引是0,
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
// HSSFSheet sheet = wookbook.getSheet("Sheet1");
Workbook book = null;
try {
book = new XSSFWorkbook(filePath);
} catch (Exception ex) {
book = new HSSFWorkbook(new FileInputStream(filePath));
}

Sheet sheet = book.getSheet("Sheet1");
//获取到Excel文件中的所有行数
int rows = sheet.getPhysicalNumberOfRows();
//遍历行
for (int i = 0; i < rows; i++) {
// 读取左上端单元格
Row row = sheet.getRow(i);
// 行不为空
if (row != null) {
//获取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells();
String value = "";
//遍历列
for (int j = 0; j < cells; j++) {
//获取到列的值
Cell cell = row.getCell(j);
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
break;
case Cell.CELL_TYPE_NUMERIC:
value += cell.getNumericCellValue() + ",";
break;
case Cell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + ",";
break;
default:
value += "0";
break;
}
}
}
// 将数据插入到mysql数据库中
String[] val = value.split(",");
for(int j=0;j<val.length;j++){
//每一行列数中的值遍历出来
System.out.println(val[j]);
}

}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?0?2
?0?2
?0?2
?0?2
?0?2

热心网友 时间:2022-05-11 21:59

直接全部在action里面写的,这个就不多说了,直接上代码:

public String executeExcel() throws Exception{ String realPath = ServletActionContext.getServletContext().getRealPath("/fileupload");
System.out.println(fileFileName);
String filePath = "";
if(this.file!=null){
File saveFile = new File(new File(realPath),this.fileFileName);
filePath = realPath+"\\"+this.fileFileName;
System.out.println(filePath);
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs(); }
FileUtils.copyFile(file, saveFile); }
this.exlToDB(filePath);
ActionContext.getContext().put("message","导入成功");
return "success"; } //读取excel2007,并把数据插入数据库
public void exlToDB(String filePath){ boolean flag = true;
AllKpi akpi = new AllKpi(); try { // 文件流指向excel文件
FileInputStream fin=new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(fin);// 创建工作薄
XSSFSheet sheet = workbook.getSheetAt(0);// 得到工作表
XSSFRow row = null;// 对应excel的行
XSSFCell cell = null;// 对应excel的列
int totalRow = sheet.getLastRowNum();// 得到excel的总记录条数
System.out.println(totalRow); // 以下的字段一一对应数据库表的字段
float idd = 0.0f;
String id = "";
String Name = "";
String DEPT_NAME = "";
String Weight = "";
<span></span>String ALGORITHM = "";
String text = " ";
//String sql = "insert into DSP_TJ_KPI values(DSP_TJ_KPI_SEQ.nextval,?,?,?,'无',?)";
for (int i = 1; i <= totalRow; i++) {
row = sheet.getRow(i);
//System.out.println(row.getCell(0).toString());
if(row.getCell(0) != null && !"".equals(row.getCell(0)) && row.getCell(1) != null && !"".equals(row.getCell(1)) && row.getCell(2) != null && !"".equals(row.getCell(2)) && row.getCell(3) != null && !"".equals(row.getCell(3))){
cell = row.getCell((short) 0);
Name = cell.toString();
System.out.println(Name);
cell = row.getCell((short) 1);
Weight = cell.toString();
System.out.println(Weight);
cell = row.getCell((short) 2);
DEPT_NAME = cell.toString();
System.out.println(DEPT_NAME);
cell = row.getCell((short) 3);
ALGORITHM = cell.toString();
System.out.println(ALGORITHM);
akpi.setAllkpiName(Name);
akpi.setAllkpiDeptName(DEPT_NAME);
akpi.setAllkpiWeight(Weight);
akpi.setAlgorithm(ALGORITHM);
akpi.setText(text);
allKpiService.addAllKpi(akpi); //以下注释代码为连接jdbc测试代码块
/*pst = con.prepareStatement(sql);
//pst.setString(1, student_id);
pst.setString
(1, DEPT_NAME);
pst.setString
(2, Name);
pst.setString
(3, Weight);
<span></span>pst.setString(4, ALGORITHM);
pst.execute();*/
System.out.println("preparestatement successful"); } }
/*pst.close();
con.close();*/
fin.close(); } catch (FileNotFoundException e)
{
flag = false;
e.printStackTrace();
}
catch (IOException ex)
{
flag = false;
ex.printStackTrace();
}

热心网友 时间:2022-05-11 23:17

Poi如何去读取excel文件

主要是通过poi去去读取文件的每一行的。下载poi的jar文件是poi-bin-3.8-beta2-20110408.zip 那下边来分享一下代码吧:
package com.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;
public class TestExcel {
//记录类的输出信息
static Log log = LogFactory.getLog(TestExcel.class);
//获取Excel文档的路径
public static String filePath = "D://excel.xls";
public static void main(String[] args) {
try {
// 创建对Excel工作簿文件
//HSSFWorkbook wookbook= new HSSFWorkbook(new FileInputStream(filePath));
// 在Excel文档中,第一张工作表的缺省索引是0,
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
// HSSFSheet sheet = wookbook.getSheet("Sheet1");
Workbook book = null;
try {
book = new XSSFWorkbook(filePath);
} catch (Exception ex) {
book = new HSSFWorkbook(new FileInputStream(filePath));
}

Sheet sheet = book.getSheet("Sheet1");
//获取到Excel文件中的所有行数
int rows = sheet.getPhysicalNumberOfRows();
//遍历行
for (int i = 0; i < rows; i++) {
// 读取左上端单元格
Row row = sheet.getRow(i);
// 行不为空
if (row != null) {
//获取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells();
String value = "";
//遍历列
for (int j = 0; j < cells; j++) {
//获取到列的值
Cell cell = row.getCell(j);
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
break;
case Cell.CELL_TYPE_NUMERIC:
value += cell.getNumericCellValue() + ",";
break;
case Cell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + ",";
break;
default:
value += "0";
break;
}
}
}
// 将数据插入到mysql数据库中
String[] val = value.split(",");
for(int j=0;j<val.length;j++){
//每一行列数中的值遍历出来
System.out.println(val[j]);
}

}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?0?2
?0?2
?0?2
?0?2
?0?2
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
粒上皇开口熟栗120g*5袋(共600g)-详细介绍 三皇王板栗介绍 品牌榜:2024年板栗十大品牌排行榜 投票结果公布【新】 为什么来大姨妈胸会胀 少儿学什么舞蹈 青年学什么舞蹈好 成年人学什么舞蹈 福州企业最低工资标准 2013年厦门的底薪是多少 生产要素的需求有哪些性质 微信视频号怎么仅自己可见 微信发表小视频时设置仅自己可见,现在要改为全部可见怎么改 一万块的高利贷,一年利息是多少? 一万一年三千利息算不算高利贷? 求乌鸫的习性?食物?详细资料? 乌鸫的特征是什么? 乌鸫雏鸟要怎么养? 老野乌鸫笼养会叫吗 DDOS是什么 乌鸫鸟经常在黎明前鸣叫 什么是DDOS 野乌鸫抓来家养什么时候叫 野乌鸫抓来家养多少才叫 现在抓的 乌鸫老野怎么开叫 流量清洗设备是否具备放ddos能力 ddos是什么? 乌鸫鸟四季都叫吗 我的乌鸫鸟小时候嘴是黄的怎么长大嘴变黑了呀,乌鸫鸟多大才开口叫呀?不会又是母的吧 乌鸫鸟什么时候叫声好听 腾讯视频选不了集数怎么办? 乌鸫鸟在什么情况下叫??怎么家里的乌鸫鸟怎么引诱也不叫呢?? java poi读一个excel总是输出最开始读的内容? poi处理Excel数据,cell的getStringCellValue()获取的值为空怎么办? 用poi怎样把excel文件里面的数据导入数据库三张关联的表中 poi读取Excel时,如果单元格设置的是数字格式,如何解决整数与小数问题 用POI读取Excel数据,运算结束后写如Excel 读取第一列和第三列的数据,计划相加之后,吧数据写入到第三列 要用Java POI读取Excel文件中的数据,并且实现对数据的格式校验,输入错误信息 poi读取复杂的Excel表格,如图 卖词网关键词交易平台卖词网的移动应用开发怎么样? 卖词网的网站服务 大学生分期有什么要求? 为什么视频发微信朋友圈自己可见,好友确看不到? 哪里有免费的信息名址交易平台? 如何看待大学生分期 易词网进行关键词交易怎么样 大学生分期付款买iphone安全吗?需要什么手续吧?对以后有什么影响吗? 卖词网的网站优势 大学生分期贷款是什么意思? 大学生分期贷款需要什么条件呀 大学生怎么分期付款,可以吗 大学生分期有哪些平台,有对此熟悉的吗