java筛选表格,java怎么获取excel中的数据_java筛选excel数据你好! 请教你个问题 java web程序如何将读取的excel表格⾥的数据插⼊到数据库,并显⽰在JSP页⾯上?
主要⽤poi.jar 包。包含两jar就可以了:poi-3.16.jar、poi-ooxml-3.16.jar
主要⽅法步:
/**
* filePath ⽂件路
* unCaseRow 要排除的⾏数(从上往下)
* unCaseLine 要排除的列数(往右)
*/
public List readExcel(String filePath, int unCaseRow, int unCaseLine) throws Exception {
Sheet sheet = null;
FileInputStream inStream = null;
try {
inStream = new FileInputStream(new File(filePath));
Workbook workBook = ate(inStream);
sheet = SheetAt(0);
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
} finally {
try {
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
List list = init(sheet, unCaseRow, unCaseLine);// 需要排除⾏数
return list;
}
// 初始化表格中的每⼀⾏,并得到每⼀个单元格的值
private List init(Sheet sheet, int unCaseRow, int unCaseLine) {
int rowNum = LastRowNum() 1; // 从零开始
List result = new ArrayList();
String[] rowArr = null;
Row row = null;
Cell cell = null;
int rowLength = 0;
int rowIndex = 0;
String rowStr = null;
for (int i = unCaseRow; i
row = Row(i);
// 每有新的⼀⾏,创建⼀个新的LinkedList对象rowLength = LastCellNum();
rowIndex = 0;
rowArr = new String[LINECOUNT];
for (int j = unCaseLine; j
cell = Cell(j);
// 获取单元格的值
rowStr = getCellValue(cell);
// 将得到的值放⼊链表中
rowArr[rowIndex ] = rowStr;
}
result.add(rowArr);
}
return result;
}
// 获取单元格的值
@SuppressWarnings("deprecation")
private String getCellValue(Cell cell) {
String cellValue = "";
DataFormatter formatter = new DataFormatter(); if (cell != null) {
// 判断单元格数据的类型,不同类型调⽤不同的⽅法switch (CellType()) {
// 数值类型
case Cell.CELL_TYPE_NUMERIC:
// 进⼀步判断 ,单元格格式是⽇期格式
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = formatter.formatCellValue(cell);
} else {
// 数值
double value = NumericCellValue();
int intValue = (int) value;
cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value); }
break;
case Cell.CELL_TYPE_STRING:
cellValue = StringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.BooleanCellValue());
break;
// 判断单元格是公式格式,需要做⼀种特殊处理来得到相应的值
case Cell.CELL_TYPE_FORMULA: {
try {
cellValue = String.NumericCellValue());
} catch (IllegalStateException e) {
cellValue = String.RichStringCellValue());
}
}
break;
case Cell.CELL_TYPE_BLANK:
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
default:
cellValue = String().trim();
java怎么编写break;
}
}
im();
}
解析成对象以后,不论是插⼊数据库,还是jsp,都是⼀样的。
插⼊数据库:hibernate、mybatis
在jsp显⽰:对象封装进list,在页⾯显⽰list。
java怎么获取excel中的数据
使⽤java poi
package webservice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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;
public class ExcelController {
@SuppressWarnings("deprecation")
public void excel() throws FileNotFoundException, IOException{
String filename = "d:\\excel.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename)); //按名引⽤excel⼯作表
// HSSFSheet sheet = Sheet("JSP");
//⽤式excel⼯作表采⽤⼯作表索引值
HSSFSheet sheet = SheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int LastRowNum();
for(int icount=0;icount
row = Row(icount);
int PhysicalNumberOfCells();
for(int j=0;j
cell1= Cell(j);
System.out.println(cell1 "--" icount "---" j);
}
}
//读取值
// System.out.StringCellValue());
//新建输流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 路径处理直接写⼊模版⽂件//存盘
workbook.write(fout);
fout.flush();
//结束关闭
fout.close();
}
public HSSFCell getCell(HSSFRow row, int index) {
// 取发期单元格
HSSFCell cell = Cell(index);
// 单元格存
if (cell == null) {
// 创建单元格
cell = ateCell(index);
}
// 返单元格
return cell;
}
public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论