使用JAVA的poi进行Excel表格的读取,以及往数据库进行数据的插入

本文展示了如何使用JAVA的POI库来读取Excel文件,并将数据插入到数据库中。通过检查文件类型,调用不同的方法读取03版和07版Excel。遍历工作表,对每一行进行处理,根据单元格类型转换数据,最后将交易记录信息存入数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,直接上码!!!

@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();

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值