概述
话不多说,直接上码!!!
@RequestMapping(path="/upload", method=RequestMethod.POST)
public void upload(@RequestParam("excelFile")Part excelFile, PrintWriter out) throws Exception{
String filename = excelFile.getSubmittedFileName();//获取文件的名字
InputStream inputStream = excelFile.getInputStream();//获取文件流
transactionRecordsService.ReadyTransactionRecords(inputStream,filename);
out.write(1);
}
//判断Excel表格是07或者是03的,因为07与03的方法不一样
@Override
public void ReadyTransactionRecords(InputStream instream,String filename) {
try {
if(filename.endsWith(EXTENSION_XLS)) {
Ready03(instream);
}else {
Ready(instream);
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
//读取Excel表格的方法
private void Ready(InputStream instream) throws IOException, ParseException {
Workbook wb =null;
try {
wb = new XSSFWorkbook(instream);//得到Excel工作簿的对象
Sheet sheet = wb.getSheetAt(0);//得到Excel工作表对象
int rowNum = sheet.getPhysicalNumberOfRows();//获得总的行数
XSSFRow rowSize = (XSSFRow) sheet.getRow(1);//获得表头的列数
int cellNum = rowSize.getPhysicalNumberOfCells();//获得没有空的列
XSSFRow row ;
for(int i=2;i<rowNum;i++){//循环所有的行
row = (XSSFRow) sheet.getRow(i);
List<String> list = new ArrayList<String>();
if(isEmptyRow(row)) {//判断行不能为空行
for(int j=0;j<cellNum;j++){//循环所有的列
String val ="";
Cell cell = row.getCell(j);
if(row.getCell(j) != null) {
switch (row.getCell(j).getCellType()) { //根据cell中的类型来输出数据
case XSSFCell.CELL_TYPE_NUMERIC:
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(row.getCell(j))) {
Date theDate = row.getCell(j).getDateCellValue();
SimpleDateFormat dff = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
val = dff.format(theDate).trim();
}else{
DecimalFormat df = new DecimalFormat("0.00");//得到保留两位小数的数值
val = df.format(row.getCell(j).getNumericCellValue());
}
break;
case XSSFCell.CELL_TYPE_STRING:
val = row.getCell(j).getStringCellValue().trim();
break;
case XSSFCell.CELL_TYPE_FORMULA:
val = row.getCell(j).getCellFormula().trim();
break;
}
list.add(val);
}else {
list.add(val);
}
}
if(isEmptyList(list)) {
TransactionRecords transactionRecords = new TransactionRecords();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
if("".equals(list.get(0).toString())) {
transactionRecords.setTradingTime(null);
}else {
transactionRecords.setTradingTime(sdf.parse(list.get(0).toString()));
}
if("".equals(list.get(1).toString())) {
transactionRecords.setIncomeAmount(0.00);
}else {
transactionRecords.setIncomeAmount(Double.valueOf(list.get(1).toString()));
}
if("".equals(list.get(2).toString())) {
transactionRecords.setPayAmount(0.00);
}else {
transactionRecords.setPayAmount(Double.valueOf(list.get(2).toString()));
}
if("".equals(list.get(3).toString())) {
transactionRecords.setAccountAmount(0.00);
}else {
transactionRecords.setAccountAmount(Double.valueOf(list.get(3).toString()));
}
transactionRecords.setBank(list.get(4).toString());
transactionRecords.setCity(list.get(5).toString());
transactionRecords.setOtherAccountNum(list.get(6).toString());
transactionRecords.setOtherAccountName(list.get(7).toString());
transactionRecords.setTradingDetail(list.get(8).toString());
//插入到数据库中
transactionRecordsDao.insertRecords(transactionRecords);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
最后
以上就是魁梧镜子为你收集整理的使用JAVA的poi进行Excel表格的读取,以及往数据库进行数据的插入的全部内容,希望文章能够帮你解决使用JAVA的poi进行Excel表格的读取,以及往数据库进行数据的插入所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复