// ����Excel public String ExcelInto() throws Exception { String directory = "/file"; String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory); File target = UploadFile.Upload(uploadFile, uploadFileFileName, targetDirectory); List<Dise> sList = new ArrayList<Dise>(); excelFile = new FileInputStream(target); Workbook wb = new HSSFWorkbook(excelFile); Sheet sheet = wb.getSheetAt(0); int rowNum = sheet.getLastRowNum() + 1; for (int i = 1; i < rowNum; i++) { Dise dise = new Dise(); Row row = sheet.getRow(i); int cellNum = row.getLastCellNum(); for (int j = 0; j < cellNum; j++) { Cell cell = row.getCell(j); String cellValue = null; switch (cell.getCellType()) { // �ж�excel��Ԫ�����ݵĸ�ʽ�����������ת�����Ա������ݿ� case 0: cellValue = String.valueOf((int) cell.getNumericCellValue()); break; case 1: cellValue = cell.getStringCellValue(); break; case 2: cellValue = String.valueOf((int) cell.getNumericCellValue()); break; case 3: cellValue = cell.getStringCellValue(); break; case 4: cellValue = cell.getStringCellValue(); break; } switch (j) { // ͨ���������ж϶�Ӧ������ֶ� case 1: dise.setName(cellValue); break; case 2: dise.setA(cellValue); break; case 3: dise.setB(cellValue); break; case 4: dise.setC(cellValue); break; } } sList.add(dise); } DiseService.add(sList); return "listAll"; }
// ����Excel public String ExcelExport() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); String ids = request.getParameter("ids"); List<Dise> list = new ArrayList<Dise>(); String[] array = ids.split(","); int[] id = new int[array.length]; for (int i = 0; i < id.length; i++) { Dise dise = DiseService.findById(Integer.valueOf(array[i])); list.add(dise); } Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("ѧ����Ϣ"); Row row = sheet.createRow(0); row.createCell(0).setCellValue("ѧ��"); row.createCell(1).setCellValue("����"); row.createCell(2).setCellValue("����"); row.createCell(3).setCellValue("�Ա�"); row.createCell(4).setCellValue("��ַ"); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); for (int i = 1; i <= list.size(); i++) { Dise stu = list.get(i - 1); row = sheet.createRow(i); row.createCell(0).setCellValue(stu.getIds()); row.createCell(1).setCellValue(stu.getName()); row.createCell(2).setCellValue(stu.getA()); row.createCell(3).setCellValue(stu.getB()); row.createCell(4).setCellValue(stu.getC()); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); excelFile = new ByteArrayInputStream(baos.toByteArray()); baos.close(); return "excel"; }