java 编程excel文件导入数据库

来源:互联网 发布:武汉mac口红专柜哪里有 编辑:程序博客网 时间:2024/06/06 00:27
// 以下为单文件上传,即excelprivate File uploadExcel; // 文件private String uploadExcelFileName; // 文件名private static String[] allowFileType = { "xls", "XLS", "xlsx", "XLSX" }; // 控制文件类型/** * excel批量导入 * */public String excelUpload() {try {if ((uploadExcelFileName == null)|| (uploadExcelFileName.equals(""))) {finalMsg = "文件名不能为空!";} else {uploadStu(uploadExcel); // 只传入一个excel文件}} catch (Exception e) {e.printStackTrace();}return SUCCESS;}private Sheet sheet;private String[][] excelValue;private int successRow;private StringBuilder msg = new StringBuilder();private String finalMsg = "";/** * excel导入的总方法 */public void uploadStu(File upload) {initExcel(upload); // 初始化boolean flag = readExcel(); // 读取if (flag) {insertIntoDB(); // 插入}}/** * 读取excel文件中数据,保存到sheet对象中 *  * @param upload */private void initExcel(File upload) {Workbook rwb = null;try {InputStream is = new FileInputStream(upload);rwb = Workbook.getWorkbook(is);sheet = rwb.getSheet(0);} catch (Exception e) {e.printStackTrace();}}/** * 读取excel中数据进入excelValue数组中 */private boolean readExcel() {excelValue = new String[sheet.getRows()][sheet.getColumns()];for (int i = 0; i < sheet.getRows(); i++) {for (int j = 0; j < sheet.getColumns(); j++) {if (i >= sheet.getRows()) {     //修改  return false;                }Cell cell = sheet.getCell(j, i);if ("".equals(cell.getContents().toString().trim())) {excelValue[i][j] = "";}if (cell.getType() == CellType.LABEL) {LabelCell labelcell = (LabelCell) cell;excelValue[i][j] = labelcell.getString().trim();} else if (cell.getType() == CellType.NUMBER) {excelValue[i][j] = cell.getContents();} else if (cell.getType() == CellType.DATE) {DateCell datcell = (DateCell) cell;excelValue[i][j] = datcell.getDate().toString();} else {excelValue[i][j] = cell.getContents().toString().trim();}}}return true;}/** * 3.保存进入数据库 *  * @param course */private void insertIntoDB() {int excelRows = excelValue.length;// 将消息清空msg.delete(0, msg.length());successRow = 0;if (excelValue.length > 1) {for (int i = 1; i < excelRows; i++) { // 从第二排开始,第一排为文字说明String[] DBValue = excelValue[i]; // 取一行数据successRow += 1;finalInsert(DBValue);}finalMsg = "录入结束:成功录入数:" + successRow + msg.toString() + "条";} else {finalMsg = "excel中无任何数据!";System.out.println("excel中没有任何数据");}}/** 数据插入数据库 **/private void finalInsert(String[] DBValue) {Building building = new Building();building.setBuildingName(DBValue[0]);building.setNamePinyin(DBValue[1]);building.setBuildingAddress(DBValue[2]);building.setSearchPinYin(DBValue[3]);building.setLat(Double.valueOf(DBValue[4]));building.setLng(Double.valueOf(DBValue[5]));building.setBusinessId(Integer.parseInt(DBValue[6]));building.setCityId(Integer.parseInt(DBValue[7]));buildingService.addBuilding(building);}
<form action="/company/building/excelUpload.action" enctype="multipart/form-data" method="post" id="uploadForm"  style="margin-left: 30px;margin-top: 10px">        <input type="file" name="uploadExcel" id="buildingId"/><input type="button" value="导入" onclick="checkFile()" class="button button-small border-blue"/><span id="tip" style="color:red;margin-left:10px;" ></span>        </form>

/** excel核对导入的格式 **/function checkFile(){var fileName = $("#buildingId").val();var ext =/\.[^\.]+/.exec(fileName);if(ext!='.xls'){$("#tip").html("请导入.xls格式的文件!");}else{$("#uploadForm").submit();}}$(function(){var result = "<s:property value='finalMsg'/>";$("#tip").html(result);});


0 0
原创粉丝点击