Java Code Examples for org.apache.poi.hssf.usermodel.HSSFSheet#autoSizeColumn()
The following examples show how to use
org.apache.poi.hssf.usermodel.HSSFSheet#autoSizeColumn() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExcelUtil.java From phone with Apache License 2.0 | 6 votes |
/** * 初始化表头 * @param sheet * @param columnJson * @param rowNumber */ private static void writeSheetHead(HSSFSheet sheet,JSONObject columnJson,int rowNumber){ if (logger.isDebugEnabled()) { logger.debug("writeSheetHead(HSSFSheet, JSONObject, int) - start"); //$NON-NLS-1$ } Set<String> keySet = columnJson.keySet(); int cellNumber = 0; HSSFRow row = sheet.createRow(rowNumber); for (String k : keySet) {//k:GOODS_NO String name = columnJson.getString(k);//品项编码 sheet.autoSizeColumn(cellNumber); HSSFCell cell = row.createCell(cellNumber++); cell.setCellValue(name); } if (logger.isDebugEnabled()) { logger.debug("writeSheetHead(HSSFSheet, JSONObject, int) - end"); //$NON-NLS-1$ } }
Example 2
Source File: ExcelExportSuper.java From phone with Apache License 2.0 | 5 votes |
/** * 写入表头 * @param sheet */ void writeHead(HSSFSheet sheet){ LinkedHashMap<String, Object> columnJson = getColumnJson(); Set<String> keySet = columnJson.keySet(); int cellNumber = 0; HSSFRow row = sheet.createRow(addRowNumber()); for (String k : keySet) { Object name = columnJson.get(k);//品项编码 sheet.autoSizeColumn(cellNumber); HSSFCell cell = row.createCell(cellNumber++); setCellValue(cell, name); pubMaxValue(k,name); } }
Example 3
Source File: ExcelUtil.java From phone with Apache License 2.0 | 5 votes |
/** * 初始化表单的列宽 * @param sheet * @param columnJson */ private static void autoSheetWidth(HSSFSheet sheet,JSONObject columnJson){ if (logger.isDebugEnabled()) { logger.debug("autoSheetWidth(HSSFSheet, JSONObject) - start"); //$NON-NLS-1$ } Set<String> keySet = columnJson.keySet(); for (int i = 0;i<keySet.size();i++) { sheet.autoSizeColumn(i); } if (logger.isDebugEnabled()) { logger.debug("autoSheetWidth(HSSFSheet, JSONObject) - end"); //$NON-NLS-1$ } }
Example 4
Source File: AgentManageServiceImpl.java From DrivingAgency with MIT License | 4 votes |
private String derivedStudentInfo(List<StudentVo> studentVos){ String agentEmail = SecurityContextHolder.getAgent().getAgentEmail(); if (StringUtils.isBlank(agentEmail)){ throw new ParamException("你的邮箱为空,不能导出Excel数据"); } HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("获取学员信息Excel表格"); HSSFRow row = null; row = sheet.createRow(0); row.setHeight((short) (26.25 * 20)); row.createCell(0).setCellValue("学员信息列表"); CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 8); sheet.addMergedRegion(rowRegion); row = sheet.createRow(1); row.setHeight((short) (22.50 * 20));//设置行高 row.createCell(0).setCellValue("学员Id");//为第一个单元格设值 row.createCell(1).setCellValue("学院学号");//为第一个单元格设值 row.createCell(2).setCellValue("学员姓名");//为第二个单元格设值 row.createCell(3).setCellValue("学员手机号");//为第三个单元格设值 row.createCell(4).setCellValue("学员身份证照片地址");//为第四个单元格设值 row.createCell(5).setCellValue("学员学费");//为第五个单元格设值 row.createCell(6).setCellValue("学员学校");//为第六个单元格设值 row.createCell(7).setCellValue("学员添加者");//为第七个单元格设值 row.createCell(8).setCellValue("学员");//为第八个单元格设值 for (int i = 0; i < studentVos.size(); i++) { row = sheet.createRow(i + 2); StudentVo studentVo = studentVos.get(i); row.createCell(0).setCellValue(studentVo.getId()); row.createCell(1).setCellValue(studentVo.getStudentId()); row.createCell(2).setCellValue(studentVo.getStudentName()); row.createCell(3).setCellValue(studentVo.getStudentPhone()); row.createCell(4).setCellValue(studentVo.getStudentImg()); if (studentVo.getStudentPrice()==null){ row.createCell(5).setCellValue(0); }else{ row.createCell(5).setCellValue(studentVo.getStudentPrice().doubleValue()); } row.createCell(6).setCellValue(studentVo.getStudentSchool()); row.createCell(7).setCellValue(studentVo.getOperator()); row.createCell(8).setCellValue(DateTimeUtil.dateToStr(studentVo.getUpdateTime())); } sheet.setDefaultRowHeight((short) (16.5 * 20)); for (int i = 0; i <= 13; i++) { sheet.autoSizeColumn(i); } try { String folder=System.getProperty("java.io.tmpdir"); File file=new File(folder,UUID.randomUUID().toString()+".xls"); if (!file.exists()){ file.createNewFile(); } wb.write(file); mailSenderUtil.sendAttachmentMail(agentEmail,"【驾校全部学员数据】", "驾校代理小程序中的全部学员数据,Excel在附件中",file); return "Excel导出成功"; } catch (IOException e) { e.printStackTrace(); } return null; }
Example 5
Source File: ExportEventsImpl.java From neoscada with Eclipse Public License 1.0 | 4 votes |
private IStatus storeExcel ( final File file, final List<Event> events, final List<Field> columns, final IProgressMonitor monitor ) throws IOException { final HSSFWorkbook workbook = new HSSFWorkbook (); final HSSFDataFormat dateFormat = workbook.createDataFormat (); final HSSFCellStyle dateCellStyle = workbook.createCellStyle (); dateCellStyle.setDataFormat ( dateFormat.getFormat ( "YYYY-MM-DD hh:mm:ss.000" ) ); try { monitor.beginTask ( Messages.ExportImpl_Progress_ExportingEvents, events.size () + 3 + columns.size () ); try { monitor.subTask ( Messages.ExportImpl_Progress_CreateWorkbook ); monitor.worked ( 1 ); final HSSFSheet sheet = createSheet ( events, workbook, columns ); monitor.worked ( 1 ); monitor.setTaskName ( Messages.ExportImpl_Progress_ExportEvents ); for ( int i = 0; i < events.size (); i++ ) { final HSSFRow row = sheet.createRow ( i + 1 ); final Event e = events.get ( i ); for ( int j = 0; j < columns.size (); j++ ) { final Field field = columns.get ( j ); final ExcelCell cell = new ExcelCell ( row, j, dateCellStyle ); field.render ( e, cell ); } monitor.worked ( 1 ); if ( monitor.isCanceled () ) { return Status.CANCEL_STATUS; } } sheet.setRepeatingRows ( new CellRangeAddress ( 0, 1, -1, -1 ) ); monitor.setTaskName ( "Auto sizing" ); for ( int i = 0; i < columns.size (); i++ ) { monitor.subTask ( String.format ( "Auto sizing column: %s", columns.get ( i ).getHeader () ) ); sheet.autoSizeColumn ( i ); monitor.worked ( 1 ); if ( monitor.isCanceled () ) { return Status.CANCEL_STATUS; } } } finally { monitor.subTask ( Messages.ExportImpl_Progress_CloseFile ); if ( workbook != null ) { makeDocInfo ( workbook ); final FileOutputStream stream = new FileOutputStream ( file ); workbook.write ( stream ); stream.close (); } monitor.worked ( 1 ); } } finally { monitor.done (); } return Status.OK_STATUS; }
Example 6
Source File: MailTests.java From spring-boot-101 with Apache License 2.0 | 4 votes |
@Test public void sendMailWithExcel() throws IOException { String[] headers = {"col1","col2","col3"}; // 声明一个工作薄 HSSFWorkbook wb = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = wb.createSheet(); HSSFRow row = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellValue(headers[i]); } int rowIndex = 1; for(int j=0; j<3; j++){ row = sheet.createRow(rowIndex); rowIndex++; HSSFCell cell1 = row.createCell(0); cell1.setCellValue(j); cell1 = row.createCell(1); cell1.setCellValue(j+1); cell1 = row.createCell(2); cell1.setCellValue(j+2); } for (int i = 0; i < headers.length; i++) { sheet.autoSizeColumn(i); } ByteArrayOutputStream os = new ByteArrayOutputStream(1000); wb.write(os); wb.close(); InputStreamSource iss = new ByteArrayResource(os.toByteArray()); os.close(); mailService.sendAttachmentsMail("[email protected]", "attachmentMail subject", "I have an attachment", iss, "abc1.xlsx"); }
Example 7
Source File: CourseLoadOverviewBean.java From fenixedu-academic with GNU Lesser General Public License v3.0 | 4 votes |
public StyledExcelSpreadsheet getInconsistencySpreadsheet() { final StyledExcelSpreadsheet spreadsheet = new StyledExcelSpreadsheet(BundleUtil.getString(Bundle.ACADEMIC, "label.course.load.inconsistency.filename") + "_" + executionSemester.getExecutionYear().getYear().replace('/', '_') + "_" + executionSemester.getSemester()); CellStyle normalStyle = spreadsheet.getExcelStyle().getValueStyle(); normalStyle.setAlignment(HorizontalAlignment.CENTER); HSSFWorkbook wb = spreadsheet.getWorkbook(); HSSFFont font = wb.createFont(); font.setColor(HSSFColor.BLACK.index); font.setFontHeightInPoints((short) 8); HSSFCellStyle redStyle = wb.createCellStyle(); redStyle.setFont(font); redStyle.setAlignment(HorizontalAlignment.CENTER); redStyle.setFillForegroundColor(HSSFColor.ORANGE.index); redStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); HSSFCellStyle yellowStyle = wb.createCellStyle(); yellowStyle.setFont(font); yellowStyle.setAlignment(HorizontalAlignment.CENTER); yellowStyle.setFillForegroundColor(HSSFColor.YELLOW.index); yellowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); spreadsheet.newHeaderRow(); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.department")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.degree")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.executionCourse")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.shift")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.shiftType")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.load.competenceCourse")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.load.curricularCourse")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.load.executionCourse")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.load.lessonInstances")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.load.lesson.count")); spreadsheet.addHeader(BundleUtil.getString(Bundle.ACADEMIC, "label.load.lessonInstances.count")); for (final ExecutionCourse executionCourse : executionSemester.getAssociatedExecutionCoursesSet()) { for (final CourseLoad courseLoad : executionCourse.getCourseLoadsSet()) { for (final Shift shift : courseLoad.getShiftsSet()) { spreadsheet.newRow(); spreadsheet.addCell(getDepartmentString(executionCourse)); spreadsheet.addCell(executionCourse.getDegreePresentationString()); spreadsheet.addCell(executionCourse.getName()); spreadsheet.addCell(shift.getNome()); spreadsheet.addCell(courseLoad.getType().getFullNameTipoAula()); final BigDecimal competenceCourseLoad = new BigDecimal(getCompetenceCourseLoad(courseLoad)).setScale(2, RoundingMode.HALF_EVEN); final BigDecimal curricularCourseLoad = new BigDecimal(getCurricularCourseLoad(courseLoad)).setScale(2, RoundingMode.HALF_EVEN); final BigDecimal executionLoad = courseLoad.getTotalQuantity().setScale(2, RoundingMode.HALF_EVEN); final BigDecimal shiftCourseLoad = getShiftCourseLoad(shift).setScale(2, RoundingMode.HALF_EVEN); if (competenceCourseLoad.signum() < 0) { spreadsheet.addCell(getCompetenceCourseLoadStrings(courseLoad), redStyle); } else { spreadsheet.addCell(competenceCourseLoad); } if (!competenceCourseLoad.equals(curricularCourseLoad) || curricularCourseLoad.signum() < 0) { spreadsheet.addCell(getCurricularCourseLoadString(courseLoad), redStyle); } else { spreadsheet.addCell(curricularCourseLoad); } if (!executionLoad.equals(curricularCourseLoad)) { spreadsheet.addCell(executionLoad, redStyle); } else { spreadsheet.addCell(executionLoad); } if (!shiftCourseLoad.equals(executionLoad)) { if (isLargeDifference(shiftCourseLoad, executionLoad, competenceCourseLoad.divide(new BigDecimal(14), 2, RoundingMode.HALF_EVEN))) { spreadsheet.addCell(shiftCourseLoad, redStyle); } else { spreadsheet.addCell(shiftCourseLoad, yellowStyle); } } else { spreadsheet.addCell(shiftCourseLoad); } spreadsheet.addCell(shift.getAssociatedLessonsSet().size()); spreadsheet.addCell(getLessonInstanceCount(shift)); } } } final HSSFSheet sheet = wb.getSheetAt(0); sheet.createFreezePane(0, 1, 0, 1); sheet.autoSizeColumn(1, true); sheet.autoSizeColumn(2, true); sheet.autoSizeColumn(3, true); sheet.autoSizeColumn(4, true); sheet.autoSizeColumn(5, true); sheet.autoSizeColumn(6, true); sheet.autoSizeColumn(7, true); sheet.autoSizeColumn(8, true); sheet.autoSizeColumn(9, true); return spreadsheet; }