- 浏览: 166525 次
- 性别:
- 来自: 重庆
最新评论
-
贝塔ZQ:
POI导入导出excel文件,个人感觉还是比较麻烦的。可以借用 ...
[转]利用POI组件 导入导出excel -
kenhy:
楼主,请教下如果类里面还有跟其他类关联怎么做?比如V_SF_S ...
自己封装的POI + Struts2 实现Excel导出工具包 -
babyphone:
下来看看哦。
自己封装的POI + Struts2 实现Excel导出工具包 -
zljjava:
...
自己封装的POI + Struts2 实现Excel导出工具包 -
guanji1989:
思路清晰,谢谢!
[原创] ExtJs + Struts2文件上传
[转]利用POI组件 导入导出excel
项目中经常遇到要将列表中的数据导出到excel中或是要将已经存在的excel文档的数据导入到数据库中。这里记录下在struts1.2中的用法。
1、从excel导入
2、从数据库导出到excel中
1、从excel导入
- /**
- * 从处部excel文件中导入应急物资记录
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward importMaterials(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response){
- int beginRowIndex = 2; //从excel中开始读取的起始行数
- int totalRows = 0; //该excel表的总行数
- IUserProfile userPro = (IUserProfile) request.getSession().getAttribute(IUserProfile.SESSION_USERPROFILE_KEY);
- ImportFileForm importFileForm = (ImportFileForm)form;
- //读取要导入的文件
- FormFile file = importFileForm.getExcelfile();
- String title = file.getFileName().replaceAll(".xls", "");
- logger.debug("标题是:"+title);
- try {
- //根据文件的输入流,创建对Excel工作簿文件的引用
- HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
- //默认excel的书页(sheet)是"Sheet1"
- HSSFSheet sheet = workbook.getSheetAt(0);
- //该excel表的总行数
- totalRows = sheet.getLastRowNum();
- logger.debug("输出总行数:"+totalRows);
- //循环读取excel表格的每行记录,并逐行进行保存
- for(int i=beginRowIndex;i<=totalRows;i++){
- HSSFRow row = sheet.getRow(i);
- //获取一行每列的数据
- HSSFCell materialNameCell = row.getCell((short)0);
- HSSFCell amountCell = row.getCell((short)1);
- HSSFCell orgCell = row.getCell((short)2);
- HSSFCell storePlaceCell = row.getCell((short)3);
- HSSFCell buyTimeCell = row.getCell((short)4);
- HSSFCell periodCell = row.getCell((short)5);
- HSSFCell principalNameCell = row.getCell((short)6);
- HSSFCell principalMobileCell = row.getCell((short)7);
- HSSFCell linkManCell = row.getCell((short)8);
- HSSFCell linkManMobileCell = row.getCell((short)9);
- //将列数据赋给相关变量
- String materialName = materialNameCell.getRichStringCellValue().getString();
- String amount = amountCell.getRichStringCellValue().getString();
- String orgName = orgCell.getRichStringCellValue().getString(); String storePlace = storePlaceCell.getRichStringCellValue().getString();
- String buyTime = "";
- if(buyTimeCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
- buyTime = new Double(buyTimeCell.getNumericCellValue()).toString();
- }else if(buyTimeCell.getCellType()==HSSFCell.CELL_TYPE_STRING){
- buyTime = buyTimeCell.getRichStringCellValue().getString();
- }
- String period = "";
- if(periodCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
- period = new Double(periodCell.getNumericCellValue()).toString();
- }else if(periodCell.getCellType()==HSSFCell.CELL_TYPE_STRING){
- period = periodCell.getRichStringCellValue().getString();
- }
- String principalName= principalNameCell.getRichStringCellValue().getString();
- String principalMobile= new Long((long)principalMobileCell.getNumericCellValue()).toString();
- String linkMan = linkManCell.getRichStringCellValue().getString();
- String linkManMobile = new Long((long)linkManMobileCell.getNumericCellValue()).toString();
- //保存当前行的数据
- try{
- saveMaterial(materialName,amount,orgName,storePlace,buyTime,period,principalName,principalMobile,linkMan,linkManMobile,userPro);
- }catch(Exception e){
- throw e;
- }
- } //for end
- MessageDisplayService.disposeMessage(request, response,"导入文件成功");
- } catch (FileNotFoundException e) {
- MessageDisplayService.disposeMessage(request, response,"找不到文件");
- } catch (IOException e) {
- MessageDisplayService.disposeMessage(request, response,"导入文件失败,连接数据库失败");
- } catch(Exception e){
- e.printStackTrace();
- MessageDisplayService.disposeMessage(request, response,"导入文件失败,文档模板格式不正确");
- }
- return mapping.findForward("import_ok");
- }
/** * 从处部excel文件中导入应急物资记录 * @param mapping * @param form * @param request * @param response * @return */ public ActionForward importMaterials(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ int beginRowIndex = 2; //从excel中开始读取的起始行数 int totalRows = 0; //该excel表的总行数 IUserProfile userPro = (IUserProfile) request.getSession().getAttribute(IUserProfile.SESSION_USERPROFILE_KEY); ImportFileForm importFileForm = (ImportFileForm)form; //读取要导入的文件 FormFile file = importFileForm.getExcelfile(); String title = file.getFileName().replaceAll(".xls", ""); logger.debug("标题是:"+title); try { //根据文件的输入流,创建对Excel工作簿文件的引用 HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream()); //默认excel的书页(sheet)是"Sheet1" HSSFSheet sheet = workbook.getSheetAt(0); //该excel表的总行数 totalRows = sheet.getLastRowNum(); logger.debug("输出总行数:"+totalRows); //循环读取excel表格的每行记录,并逐行进行保存 for(int i=beginRowIndex;i<=totalRows;i++){ HSSFRow row = sheet.getRow(i); //获取一行每列的数据 HSSFCell materialNameCell = row.getCell((short)0); HSSFCell amountCell = row.getCell((short)1); HSSFCell orgCell = row.getCell((short)2); HSSFCell storePlaceCell = row.getCell((short)3); HSSFCell buyTimeCell = row.getCell((short)4); HSSFCell periodCell = row.getCell((short)5); HSSFCell principalNameCell = row.getCell((short)6); HSSFCell principalMobileCell = row.getCell((short)7); HSSFCell linkManCell = row.getCell((short)8); HSSFCell linkManMobileCell = row.getCell((short)9); //将列数据赋给相关变量 String materialName = materialNameCell.getRichStringCellValue().getString(); String amount = amountCell.getRichStringCellValue().getString(); String orgName = orgCell.getRichStringCellValue().getString(); String storePlace = storePlaceCell.getRichStringCellValue().getString(); String buyTime = ""; if(buyTimeCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){ buyTime = new Double(buyTimeCell.getNumericCellValue()).toString(); }else if(buyTimeCell.getCellType()==HSSFCell.CELL_TYPE_STRING){ buyTime = buyTimeCell.getRichStringCellValue().getString(); } String period = ""; if(periodCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){ period = new Double(periodCell.getNumericCellValue()).toString(); }else if(periodCell.getCellType()==HSSFCell.CELL_TYPE_STRING){ period = periodCell.getRichStringCellValue().getString(); } String principalName= principalNameCell.getRichStringCellValue().getString(); String principalMobile= new Long((long)principalMobileCell.getNumericCellValue()).toString(); String linkMan = linkManCell.getRichStringCellValue().getString(); String linkManMobile = new Long((long)linkManMobileCell.getNumericCellValue()).toString(); //保存当前行的数据 try{ saveMaterial(materialName,amount,orgName,storePlace,buyTime,period,principalName,principalMobile,linkMan,linkManMobile,userPro); }catch(Exception e){ throw e; } } //for end MessageDisplayService.disposeMessage(request, response,"导入文件成功"); } catch (FileNotFoundException e) { MessageDisplayService.disposeMessage(request, response,"找不到文件"); } catch (IOException e) { MessageDisplayService.disposeMessage(request, response,"导入文件失败,连接数据库失败"); } catch(Exception e){ e.printStackTrace(); MessageDisplayService.disposeMessage(request, response,"导入文件失败,文档模板格式不正确"); } return mapping.findForward("import_ok"); }
- /**
- * 将从excel中抽取的一行物资记录转换后存入数据库
- * @param userPro
- */
- private void saveMaterial(String materialName,
- String amount,
- String orgName,
- String storePlace,
- String buyTime,
- String period,
- String principalName,
- String principalMobile,
- String linkMan,
- String linkManMobile,
- IUserProfile userPro) throws Exception{
- MaterialForm form = new MaterialForm();
- form.setMaterialName(materialName);
- form.setAmount(amount);
- form.setOrgName(orgName);
- form.setStorePlace(storePlace);
- form.setBuyTime(buyTime);
- form.setPeriod(period);
- form.setPrincipalName(principalName);
- form.setPrincipalMobile(principalMobile);
- form.setLinkman(linkMan);
- form.setLinkmanMobile(linkManMobile);
- //保存
- materialManager.saveOrUpdate(form, userPro);
- }
/** * 将从excel中抽取的一行物资记录转换后存入数据库 * @param userPro */ private void saveMaterial(String materialName, String amount, String orgName, String storePlace, String buyTime, String period, String principalName, String principalMobile, String linkMan, String linkManMobile, IUserProfile userPro) throws Exception{ MaterialForm form = new MaterialForm(); form.setMaterialName(materialName); form.setAmount(amount); form.setOrgName(orgName); form.setStorePlace(storePlace); form.setBuyTime(buyTime); form.setPeriod(period); form.setPrincipalName(principalName); form.setPrincipalMobile(principalMobile); form.setLinkman(linkMan); form.setLinkmanMobile(linkManMobile); //保存 materialManager.saveOrUpdate(form, userPro); }
2、从数据库导出到excel中
- /**
- * 根据条件导出excel(可选择导出项)
- * @param form
- * @param request
- * @param response
- * @throws IOException
- */
- public void exportStreetEvent(ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException{
- HSSFWorkbook workbook = new HSSFWorkbook();
- int sheetRowNum = 0;
- XwzsEventQueryForm xwzsEventQueryForm = (XwzsEventQueryForm)form;
- /**从库中取出相关数据**/
- List list12 = xwzsEventManager.getEventQuery(xwzsEventQueryForm,projections);
- List list = getEventExport(list12); //将取出的列表格式化,不是必须
- String title = "事件列表";
- // 创建工作表和标题
- HSSFSheet sheet = workbook.createSheet("event");
- // 设置标题栏合并区域
- Region r = new Region(0, (short) 0, 0, (short) 12);
- ExportUtil.fillMergedRegion(workbook, sheet, r, title, workbook.createCellStyle());
- sheetRowNum++;
- // 设置列宽
- // 默认列宽
- sheet.setDefaultColumnWidth((short) 10);
- // 自定义列宽
- // sheet.autoSizeColumn((short)1);
- sheet.setColumnWidth((short)1, (short)(268*21));
- sheet.setColumnWidth((short)3, (short)(268*13));
- sheet.setColumnWidth((short)10, (short)(268*21));
- sheet.setColumnWidth((short)11, (short)(268*16));
- // 设置字体格式
- HSSFFont font = workbook.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
- font.setFontName("仿宋_GB2312");
- font.setFontHeightInPoints((short) 12);
- // 创建格式模型
- HSSFCellStyle cs = workbook.createCellStyle();
- cs.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
- cs.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
- cs.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
- cs.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
- // 自动换行
- cs.setWrapText(true);
- // 上下居中
- cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cs.setFont(font);
- // 创建数据项格式
- HSSFCellStyle cs1 = workbook.createCellStyle();
- cs1.setBorderTop(HSSFCellStyle.BORDER_THIN);
- cs1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
- cs1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- cs1.setBorderRight(HSSFCellStyle.BORDER_THIN);
- cs1.setWrapText(true);
- cs1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- String[] titles = { "日期", "期号", "事发时间", "首报时间", "首报单位","地点","事件标题","类别","等级","领导现场指挥","区领导批示","报市应急办","处置中存在的问题","首报人","接电人" };
- HSSFRow dataTitleRow = sheet.createRow((short) sheetRowNum++);
- //获取用户选择的输出项
- for (int i = 0; i < titles.length; i++) {
- HSSFCell cell = dataTitleRow.createCell((short) i);
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- cell.setCellValue(new HSSFRichTextString(titles[i]));
- cell.setCellStyle(cs);
- }
- // 数据模型转换:创建表格数据
- for (int i = 0; i < list.size(); i++) {
- EventExport eventexport = (EventExport) list.get(i);
- HSSFRow dataRow = sheet.createRow((short) (i+sheetRowNum));
- HSSFCell celldate = dataRow.createCell((short) 0);
- HSSFCell cellqihao = dataRow.createCell((short) 1);
- HSSFCell cellhappentime = dataRow.createCell((short) 2);
- HSSFCell cellreporttime = dataRow.createCell((short) 3);
- HSSFCell cellreportdepart = dataRow.createCell((short) 4);
- HSSFCell celladdress = dataRow.createCell((short) 5);
- HSSFCell cellevent = dataRow.createCell((short) 6);
- HSSFCell celltype = dataRow.createCell((short) 7);
- HSSFCell cellgrade = dataRow.createCell((short) 8);
- HSSFCell cellcommand = dataRow.createCell((short) 9);
- HSSFCell cell1denote = dataRow.createCell((short) 10);
- HSSFCell cell1report = dataRow.createCell((short) 11);
- HSSFCell cell1problem = dataRow.createCell((short) 12);
- // 中文编码
- celldate.setCellValue(new HSSFRichTextString(eventexport.getDate()));
- cell2.setCellValue(new HSSFRichTextString(jinzhan.getDetailcontent()));
- cellhappentime.setCellValue(new HSSFRichTextString(eventexport.getHappentime()));
- cellreporttime.setCellValue(new HSSFRichTextString(eventexport.getReporttime()));
- cellreportdepart.setCellValue(new HSSFRichTextString(eventexport.getDepartment()));
- celladdress.setCellValue(new HSSFRichTextString(eventexport.getPlace()));
- cellevent.setCellValue(new HSSFRichTextString(eventexport.getEventcontent()));
- celltype.setCellValue(new HSSFRichTextString(eventexport.getEventtype()));
- cellgrade.setCellValue(new HSSFRichTextString(eventexport.getEventlevel()));
- cellcommand.setCellValue(new HSSFRichTextString(eventexport.getLocalleader()));
- cell1denote.setCellValue(new HSSFRichTextString(eventexport.getDenote()));
- cell1report.setCellValue(new HSSFRichTextString(eventexport.getReportflag()));
- // 设置各个列的格式
- celldate.setCellStyle(cs1);
- cellqihao.setCellStyle(cs1);
- cellhappentime.setCellStyle(cs1);
- cellreporttime.setCellStyle(cs1);
- cellreportdepart.setCellStyle(cs1);
- celladdress.setCellStyle(cs1);
- cellevent.setCellStyle(cs1);
- celltype.setCellStyle(cs1);
- cellgrade.setCellStyle(cs1);
- cellcommand.setCellStyle(cs1);
- cell1denote.setCellStyle(cs1);
- cell1report.setCellStyle(cs1);
- cell1problem.setCellStyle(cs1);
- }
- //输出文件
- OutputStream out = response.getOutputStream();
- response.setCharacterEncoding("gbk");
- response.setContentType("application/x-msexcel");
- response.setHeader("Content-Disposition", "attachment;filename="
- + ExportUtil.UniC(title) + ".xls");
- workbook.write(out);
- out.close();
- }
/** * 根据条件导出excel(可选择导出项) * @param form * @param request * @param response * @throws IOException */ public void exportStreetEvent(ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException{ HSSFWorkbook workbook = new HSSFWorkbook(); int sheetRowNum = 0; XwzsEventQueryForm xwzsEventQueryForm = (XwzsEventQueryForm)form; /**从库中取出相关数据**/ List list12 = xwzsEventManager.getEventQuery(xwzsEventQueryForm,projections); List list = getEventExport(list12); //将取出的列表格式化,不是必须 String title = "事件列表"; // 创建工作表和标题 HSSFSheet sheet = workbook.createSheet("event"); // 设置标题栏合并区域 Region r = new Region(0, (short) 0, 0, (short) 12); ExportUtil.fillMergedRegion(workbook, sheet, r, title, workbook.createCellStyle()); sheetRowNum++; // 设置列宽 // 默认列宽 sheet.setDefaultColumnWidth((short) 10); // 自定义列宽 // sheet.autoSizeColumn((short)1); sheet.setColumnWidth((short)1, (short)(268*21)); sheet.setColumnWidth((short)3, (short)(268*13)); sheet.setColumnWidth((short)10, (short)(268*21)); sheet.setColumnWidth((short)11, (short)(268*16)); // 设置字体格式 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); font.setFontName("仿宋_GB2312"); font.setFontHeightInPoints((short) 12); // 创建格式模型 HSSFCellStyle cs = workbook.createCellStyle(); cs.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); cs.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); cs.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); cs.setBorderRight(HSSFCellStyle.BORDER_MEDIUM); // 自动换行 cs.setWrapText(true); // 上下居中 cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cs.setFont(font); // 创建数据项格式 HSSFCellStyle cs1 = workbook.createCellStyle(); cs1.setBorderTop(HSSFCellStyle.BORDER_THIN); cs1.setBorderLeft(HSSFCellStyle.BORDER_THIN); cs1.setBorderBottom(HSSFCellStyle.BORDER_THIN); cs1.setBorderRight(HSSFCellStyle.BORDER_THIN); cs1.setWrapText(true); cs1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); String[] titles = { "日期", "期号", "事发时间", "首报时间", "首报单位","地点","事件标题","类别","等级","领导现场指挥","区领导批示","报市应急办","处置中存在的问题","首报人","接电人" }; HSSFRow dataTitleRow = sheet.createRow((short) sheetRowNum++); //获取用户选择的输出项 for (int i = 0; i < titles.length; i++) { HSSFCell cell = dataTitleRow.createCell((short) i); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(new HSSFRichTextString(titles[i])); cell.setCellStyle(cs); } // 数据模型转换:创建表格数据 for (int i = 0; i < list.size(); i++) { EventExport eventexport = (EventExport) list.get(i); HSSFRow dataRow = sheet.createRow((short) (i+sheetRowNum)); HSSFCell celldate = dataRow.createCell((short) 0); HSSFCell cellqihao = dataRow.createCell((short) 1); HSSFCell cellhappentime = dataRow.createCell((short) 2); HSSFCell cellreporttime = dataRow.createCell((short) 3); HSSFCell cellreportdepart = dataRow.createCell((short) 4); HSSFCell celladdress = dataRow.createCell((short) 5); HSSFCell cellevent = dataRow.createCell((short) 6); HSSFCell celltype = dataRow.createCell((short) 7); HSSFCell cellgrade = dataRow.createCell((short) 8); HSSFCell cellcommand = dataRow.createCell((short) 9); HSSFCell cell1denote = dataRow.createCell((short) 10); HSSFCell cell1report = dataRow.createCell((short) 11); HSSFCell cell1problem = dataRow.createCell((short) 12); // 中文编码 celldate.setCellValue(new HSSFRichTextString(eventexport.getDate())); cell2.setCellValue(new HSSFRichTextString(jinzhan.getDetailcontent())); cellhappentime.setCellValue(new HSSFRichTextString(eventexport.getHappentime())); cellreporttime.setCellValue(new HSSFRichTextString(eventexport.getReporttime())); cellreportdepart.setCellValue(new HSSFRichTextString(eventexport.getDepartment())); celladdress.setCellValue(new HSSFRichTextString(eventexport.getPlace())); cellevent.setCellValue(new HSSFRichTextString(eventexport.getEventcontent())); celltype.setCellValue(new HSSFRichTextString(eventexport.getEventtype())); cellgrade.setCellValue(new HSSFRichTextString(eventexport.getEventlevel())); cellcommand.setCellValue(new HSSFRichTextString(eventexport.getLocalleader())); cell1denote.setCellValue(new HSSFRichTextString(eventexport.getDenote())); cell1report.setCellValue(new HSSFRichTextString(eventexport.getReportflag())); // 设置各个列的格式 celldate.setCellStyle(cs1); cellqihao.setCellStyle(cs1); cellhappentime.setCellStyle(cs1); cellreporttime.setCellStyle(cs1); cellreportdepart.setCellStyle(cs1); celladdress.setCellStyle(cs1); cellevent.setCellStyle(cs1); celltype.setCellStyle(cs1); cellgrade.setCellStyle(cs1); cellcommand.setCellStyle(cs1); cell1denote.setCellStyle(cs1); cell1report.setCellStyle(cs1); cell1problem.setCellStyle(cs1); } //输出文件 OutputStream out = response.getOutputStream(); response.setCharacterEncoding("gbk"); response.setContentType("application/x-msexcel"); response.setHeader("Content-Disposition", "attachment;filename=" + ExportUtil.UniC(title) + ".xls"); workbook.write(out); out.close(); }
- /**
- * 添加标题栏的内容和设置标题格式
- *
- * @param workbook
- * @param sheet
- * @param region
- * @param text
- * @param cs
- */
- public static void fillMergedRegion(HSSFWorkbook workbook, HSSFSheet sheet,
- Region region, String text, HSSFCellStyle cs) {
- cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- setRegionStyle(workbook, sheet, region, cs);
- HSSFRow row;
- HSSFCell cell;
- // 设置字体格式
- HSSFFont font = workbook.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setFontHeightInPoints((short) 18);
- // 居中显示
- cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- cs.setFont(font);
- row = sheet.getRow(region.getRowFrom());
- cell = row.getCell(region.getColumnFrom());
- cell.setCellValue(new HSSFRichTextString(text));
- sheet.addMergedRegion(region);
- }
/** * 添加标题栏的内容和设置标题格式 * * @param workbook * @param sheet * @param region * @param text * @param cs */ public static void fillMergedRegion(HSSFWorkbook workbook, HSSFSheet sheet, Region region, String text, HSSFCellStyle cs) { cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); setRegionStyle(workbook, sheet, region, cs); HSSFRow row; HSSFCell cell; // 设置字体格式 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 18); // 居中显示 cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); cs.setFont(font); row = sheet.getRow(region.getRowFrom()); cell = row.getCell(region.getColumnFrom()); cell.setCellValue(new HSSFRichTextString(text)); sheet.addMergedRegion(region); }
- public static void setRegionStyle(HSSFWorkbook workbook, HSSFSheet sheet,
- Region region, HSSFCellStyle cs) {
- for (int i = region.getRowFrom(); i <= region.getRowTo(); i++) {
- HSSFRow row = HSSFCellUtil.getRow(i, sheet);
- for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
- HSSFCell cell = HSSFCellUtil.getCell(row, (short) j);
- cell.setCellStyle(cs);
- }
- }
- }
评论
1 楼
贝塔ZQ
2017-01-05
POI导入导出excel文件,个人感觉还是比较麻烦的。可以借用插件实现的,pageoffice插件实现导入导出excel就很简单,导入 document.getElementById("PageOfficeCtrl1").ExcelImportDialog();就可以实现;导出的话,直接另存到本地就可以的。可以试试看
相关推荐
标题"利用poi实现导入导出Excel工具类"指出我们将探讨如何利用Apache POI来创建一个工具类,用于在Java应用程序中方便地进行Excel文件的导入和导出操作。 Apache POI库提供了HSSF(处理.xls文件)和XSSF(处理.xlsx...
Java POI 实现 Excel 导入导出 Java POI 是一个流行的 Java 库,用于处理 Microsoft Office 文件格式,包括 Excel 文件。在本文中,我们将详细介绍如何使用 Java POI 实现 Excel 导入导出功能。 1. 什么是 Java ...
总之,这个项目是一个完整的Java解决方案,用于处理Excel文件的导入导出,同时具备用户友好的进度条显示。它利用了Apache POI的强大功能,结合maven的依赖管理,以及可能的GUI组件,提供了高效且直观的数据操作体验...
总的来说,"SpringBoot + Mybatis + POI导入、导出Excel文件"的项目是一个实用的工具,可以帮助开发者快速实现数据交换功能,提高工作效率。通过理解SpringBoot的自动配置、Mybatis的SQL映射以及POI的Excel操作,...
1. **导出Excel**:首先,使用MyBatis执行SQL查询获取数据,然后通过POI创建一个Workbook对象,代表Excel工作簿。接着,创建Sheet对象表示工作表,并在其中添加Row和Cell来填充数据。最后,将Workbook写入到本地文件...
导出Excel的过程则相反。首先,你需要创建一个新的Workbook对象,然后创建Sheet和Row,填充Cell数据。最后,使用Workbook的write方法将数据写入到OutputStream中,可以是文件流或内存流,从而生成Excel文件。 除了...
标题提到的"poi导入导出Excel表格的所以jar包"指的是使用Apache POI库进行Excel操作所需的依赖库。 1. **poi-3.8-20120326.jar**: 这是Apache POI的主要核心库,包含了处理Excel文件的核心类和方法。例如,你可以...
在导入导出Excel时,我们需要以下几个关键步骤: 1. **创建Workbook对象**:这是Excel工作簿的基础,可以是HSSFWorkbook(.xls)或XSSFWorkbook(.xlsx)。 2. **创建Sheet对象**:代表Excel工作表,每个Workbook...
导出Excel文件: 1. **创建Workbook和Sheet**:首先,你需要创建一个新的Workbook实例,然后根据需求创建Sheet。 2. **添加Row和Cell**:在Sheet上添加Row,然后在Row中添加Cell。可以通过`Sheet.createRow(int ...
标题中的“POI千万级导入导出EXCEL工具”指的是使用Apache POI库来处理大量数据的Excel导入和导出功能。Apache POI是Java语言中用于读写Microsoft Office格式档案的开源库,特别是Excel(XLS和XLSX)文件。在大数据...
标题提到的"使用poi导入导出excel的相关包.rar"是一个包含多种POI库的压缩包,适用于进行Excel文件的导入和导出操作。 该压缩包中的文件包括: 1. poi-ooxml-full-5.0.0.jar 和 poi-ooxml-lite-5.0.0.jar:这两个都...
利用开源组件POI3.0.2,用java语言实现了动态导出Excel文档,并且也实现了Excel文档的导入。有完整的例子,可直接使用。ExportExcel类为导出Excel报表通用类。 1.将源代码导入eclipse中 2.部署项目,启动tomcat...
这个“poi导入导出excel”的资源可能是针对初学者的一个教程或示例集合,帮助他们理解和掌握如何利用 Apache POI 进行 Excel 的导入与导出操作。 Apache POI 的核心组件包括 HSSF(Horrible Spreadsheet Format)和...
利用开源组件POI3.0.2,用java语言实现了动态导出Excel文档,并且也实现了Excel文档的导入。有完整的例子,可直接使用。ExportExcel类为导出Excel报表通用类。 1.将源代码导入eclipse中 2.部署项目,启动tomcat...
使用POI导出Excel文件** 导出Excel文件主要涉及以下几个步骤: - **创建Workbook对象**:这是Excel文件的容器,可以使用`XSSFWorkbook`(针对.xlsx)或`HSSFWorkbook`(针对.xls)创建。 - **创建Sheet对象**:...
在标题和描述中提到的"poi方式导出excel需要的3个jar包",通常指的是以下三个核心组件: 1. **poi-ooxml-schemas**: 这个JAR包包含了Microsoft Office Open XML (OOXML) 的XML架构定义,用于解析和创建XLSX文件。...
本项目提供的"excelpoi导入导出功能"就是基于Apache POI实现的,它支持使用注解进行数据处理,这为开发人员提供了更加灵活和便捷的方式。 Apache POI的核心组件包括SXSSF(Streaming Usermodel API)和HSSF/XSSF...
在进行批量导入和导出Excel数据时,Apache POI是一个非常实用的选择。以下是关于使用Java POI进行Excel操作的关键知识点: 1. **Apache POI介绍**: Apache POI是一个开源项目,它提供了API来处理Microsoft的文件...
在导入导出Excel的场景中,SpringMVC主要负责接收HTTP请求,处理业务逻辑,并将结果返回给用户。 Hibernate是Java领域广泛使用的ORM框架,它允许开发者用面向对象的方式来操作数据库。通过Hibernate,开发者可以将...
标题中的“POI导入导出excel jar”是指Apache POI项目,这是一个开源的Java库,专门用于处理Microsoft Office格式的文件,尤其是Excel。在描述中提到的“只有两个包”,指的是可能提供的压缩文件包含了Apache POI的...