org.apache.poi.hssf.usermodel.HSSFWorkbook.createDataFormat()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(326)

本文整理了Java中org.apache.poi.hssf.usermodel.HSSFWorkbook.createDataFormat()方法的一些代码示例,展示了HSSFWorkbook.createDataFormat()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HSSFWorkbook.createDataFormat()方法的具体详情如下:
包路径:org.apache.poi.hssf.usermodel.HSSFWorkbook
类名称:HSSFWorkbook
方法名:createDataFormat

HSSFWorkbook.createDataFormat介绍

[英]Returns the instance of HSSFDataFormat for this workbook.
[中]返回此工作簿的HSSFDataFormat实例。

代码示例

代码示例来源:origin: org.apache.poi/poi

  1. @Override
  2. public HSSFDataFormat createDataFormat() {
  3. return workbook.createDataFormat();
  4. }

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.poi

  1. @Override
  2. public HSSFDataFormat createDataFormat() {
  3. return workbook.createDataFormat();
  4. }

代码示例来源:origin: stackoverflow.com

  1. HSSFWorkbook wb = ...;
  2. DataFormat format = wb.createDataFormat();
  3. HSSFCellStyle cellStyle = wb.createCellStyle();
  4. style.setDataFormat(format.getFormat("@")); // or "text"
  5. cell.setCellStyle(cellStyle);

代码示例来源:origin: QihooTest/Leo

  1. protected void setNumericCell(HSSFCell cell, BigDecimal value, HSSFWorkbook workbook)
  2. {
  3. if(logger.isDebugEnabled())
  4. logger.debug("setNumericCell(cell={}, value={}, workbook={}) - start",
  5. new Object[] {cell, value, workbook} );
  6. cell.setCellValue( ((BigDecimal)value).doubleValue() );
  7. HSSFDataFormat df = workbook.createDataFormat();
  8. int scale = ((BigDecimal)value).scale();
  9. short format;
  10. if(scale <= 0){
  11. format = df.getFormat("####");
  12. }
  13. else {
  14. String zeros = createZeros(((BigDecimal)value).scale());
  15. format = df.getFormat("####." + zeros);
  16. }
  17. if(logger.isDebugEnabled())
  18. logger.debug("Using format '{}' for value '{}'.", String.valueOf(format), value);
  19. HSSFCellStyle cellStyleNumber = workbook.createCellStyle();
  20. cellStyleNumber.setDataFormat(format);
  21. cell.setCellStyle(cellStyleNumber);
  22. }

代码示例来源:origin: QihooTest/Leo

  1. protected static HSSFCellStyle createDateCellStyle(HSSFWorkbook workbook) {
  2. HSSFDataFormat format = workbook.createDataFormat();
  3. short dateFormatCode = format.getFormat(DATE_FORMAT_AS_NUMBER_DBUNIT);
  4. HSSFCellStyle dateCellStyle = workbook.createCellStyle();
  5. dateCellStyle.setDataFormat(dateFormatCode);
  6. return dateCellStyle;
  7. }

代码示例来源:origin: jasperreports/jasperreports

  1. protected void openWorkbook(OutputStream os)
  2. {
  3. workbook = new HSSFWorkbook();
  4. emptyCellStyle = workbook.createCellStyle();
  5. emptyCellStyle.setFillForegroundColor((new HSSFColor.WHITE()).getIndex());
  6. emptyCellStyle.setFillPattern(backgroundMode);
  7. dataFormat = workbook.createDataFormat();
  8. }

代码示例来源:origin: stackoverflow.com

  1. FileOutputStream out = new FileOutputStream("dateFormat.xls");
  2. HSSFWorkbook hssfworkbook = new HSSFWorkbook();
  3. HSSFSheet sheet = hssfworkbook.createSheet("new sheet");
  4. HSSFCellStyle cs = hssfworkbook.createCellStyle();
  5. HSSFDataFormat df = hssfworkbook.createDataFormat();
  6. cs.setDataFormat(df.getFormat("#,##0.0"));
  7. for(int i=0 ;i <100 ; i++ )
  8. {
  9. double value = new Random(i).nextGaussian();
  10. HSSFRow row = sheet.createRow((short) i);
  11. HSSFCell cell = row.createCell((short) 0);
  12. cell.setCellValue(value);
  13. if(value>=0 && value<=1)
  14. cell.setCellStyle(cs);
  15. }
  16. hssfworkbook.write(out);
  17. out.close();

代码示例来源:origin: qcadoo/mes

  1. private void init(HSSFWorkbook workbook) {
  2. dataFormat = workbook.createDataFormat();
  3. firstLeftWhite = createStyle(workbook, true, LEFT);
  4. firstRightWhite = createStyle(workbook, true, RIGHT);
  5. leftWhite = createStyle(workbook, false, LEFT);
  6. rightWhite = createStyle(workbook, false, RIGHT);
  7. }

代码示例来源:origin: stackoverflow.com

  1. HSSFSheet sheet = wb.createSheet("format sheet");
  2. CellStyle style;
  3. DataFormat format = wb.createDataFormat();
  4. short rowNum = 0;
  5. short colNum = 0;

代码示例来源:origin: stackoverflow.com

  1. try{
  2. FileOutputStream out = new FileOutputStream
  3. ("dateFormat.xls");
  4. HSSFWorkbook hssfworkbook = new HSSFWorkbook();
  5. HSSFSheet sheet = hssfworkbook.createSheet
  6. ("new sheet");
  7. HSSFCellStyle cs = hssfworkbook.createCellStyle();
  8. HSSFDataFormat df = hssfworkbook.
  9. createDataFormat();
  10. cs.setDataFormat(df.getFormat("#,##0.0"));
  11. HSSFRow row = sheet.createRow((short)0);
  12. HSSFCell cell = row.createCell((short)0);
  13. cell.setCellValue(11111.1);
  14. cell.setCellStyle(cs);
  15. hssfworkbook.write(out);
  16. out.close();
  17. }catch(Exception e){}
  18. }

代码示例来源:origin: stackoverflow.com

  1. private static HSSFWorkbook workbook;
  2. public static void main( String args[] ) throws IOException {
  3. workbook = new HSSFWorkbook();
  4. HSSFSheet sheet = workbook.createSheet( "sheet" );
  5. HSSFRow row = sheet.createRow( 0 );
  6. writeDecimal( row, 0.0781013, 0 );
  7. FileOutputStream fos = new FileOutputStream( "workbook.xls", false );
  8. workbook.write( fos );
  9. fos.close();
  10. }
  11. private static void writeDecimal( HSSFRow row, Double data, int position ) {
  12. String pattern = "#.0000000000";
  13. HSSFCell celda = row.createCell(position);
  14. CellStyle styleDecimal = workbook.createCellStyle(); // Font and alignment
  15. styleDecimal.setDataFormat(workbook.createDataFormat().getFormat(pattern));
  16. celda.setCellStyle(styleDecimal);
  17. celda.setCellType(Cell.CELL_TYPE_NUMERIC);
  18. celda.setCellValue(data);
  19. }

代码示例来源:origin: stackoverflow.com

  1. public static void main(String[] args) throws JDOMException, IOException {
  2. File excelFile = new File("C:/temp/test.xls");
  3. HSSFWorkbook workbook = new HSSFWorkbook();
  4. HSSFSheet sheet = workbook.createSheet("sheetname");
  5. Row r = null;
  6. CellStyle cellStyle = workbook.createCellStyle();
  7. // CreationHelper createHelper = workbook.getCreationHelper();
  8. cellStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
  9. for (int i = 0; i < 2; i++) {
  10. switch (i) {
  11. case 0:
  12. r = sheet.createRow(0);
  13. r.setRowStyle(cellStyle);
  14. break;
  15. case 1:
  16. r = sheet.createRow(1);
  17. r.setRowStyle(cellStyle);
  18. break;
  19. default:
  20. break;
  21. }
  22. }
  23. try (FileOutputStream out = new FileOutputStream(excelFile)) {
  24. workbook.write(out);
  25. }
  26. Desktop.getDesktop().open(excelFile);
  27. }

代码示例来源:origin: stackoverflow.com

  1. HSSFDataFormat dataFormat = wb.createDataFormat();
  2. currencyStyle.setDataFormat(dataFormat.getFormat("$#,##0.00.000"));

代码示例来源:origin: qcadoo/mes

  1. StylesContainer(HSSFWorkbook workbook, FontsContainer fontsContainer) {
  2. regularStyle = workbook.createCellStyle();
  3. regularStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  4. headerStyle = workbook.createCellStyle();
  5. headerStyle.setFont(fontsContainer.boldFont);
  6. headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  7. headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  8. headerStyle.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM);
  9. headerStyle.setWrapText(true);
  10. timeStyle = workbook.createCellStyle();
  11. timeStyle.setDataFormat(workbook.createDataFormat().getFormat("[HH]:MM:SS"));
  12. timeBoldStyle = workbook.createCellStyle();
  13. timeBoldStyle.setDataFormat(workbook.createDataFormat().getFormat("[HH]:MM:SS"));
  14. timeBoldStyle.setFont(fontsContainer.boldFont);
  15. negativeTimeStyle = workbook.createCellStyle();
  16. negativeTimeStyle.setDataFormat(workbook.createDataFormat().getFormat("-[HH]:MM:SS"));
  17. numberStyle = workbook.createCellStyle();
  18. numberStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00###"));
  19. numberBoldStyle = workbook.createCellStyle();
  20. numberBoldStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00###"));
  21. numberBoldStyle.setFont(fontsContainer.boldFont);
  22. dateTimeStyle = workbook.createCellStyle();
  23. dateTimeStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-mm-dd hh:mm"));
  24. }

代码示例来源:origin: org.databene/databene-formats

  1. private void writeHeaderRow(E bean, HSSFSheet sheet) {
  2. HSSFRow headerRow = sheet.createRow(0);
  3. for (int i = 0; i < beanProperties.size(); i++) {
  4. PropFormat prop = beanProperties.get(i);
  5. // write column header
  6. String componentName = prop.getName();
  7. headerRow.createCell(i).setCellValue(new HSSFRichTextString(componentName));
  8. // apply pattern
  9. if (prop.getPattern() != null) {
  10. HSSFDataFormat dataFormat = workbook.createDataFormat();
  11. CellStyle columnStyle = workbook.createCellStyle();
  12. columnStyle.setDataFormat(dataFormat.getFormat(prop.getPattern()));
  13. sheet.setDefaultColumnStyle(i, columnStyle);
  14. }
  15. }
  16. }

代码示例来源:origin: stackoverflow.com

  1. FileOutputStream out = new FileOutputStream("dateFormat.xls");
  2. HSSFWorkbook hssfworkbook = new HSSFWorkbook();
  3. HSSFSheet sheet = hssfworkbook.createSheet("new sheet");
  4. HSSFCellStyle cs = hssfworkbook.createCellStyle();
  5. HSSFDataFormat df = hssfworkbook.createDataFormat();
  6. HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(hssfworkbook);
  7. cs.setDataFormat(df.getFormat("h:mm:ss"));
  8. HSSFRow row = sheet.createRow((short)0);
  9. HSSFCell cell = row.createCell((short)0);
  10. cell.setCellFormula("TIME(0,3,24)");//this method only sets the formula string and does not calculate the formula value
  11. cell.setCellType(Cell.CELL_TYPE_FORMULA);//Set the cells type (numeric, formula or string)
  12. evaluator.evaluateFormulaCell(cell);// it evaluates the formula, and saves the result of the formula
  13. cell.setCellStyle(cs);
  14. HSSFRow row2 = sheet.createRow((short)1);
  15. HSSFCell cell2 = row2.createCell((short)0);
  16. cell2.setCellFormula("TIME(0,9,54)");
  17. cell2.setCellType(Cell.CELL_TYPE_FORMULA);
  18. evaluator.evaluateFormulaCell(cell2);
  19. cell2.setCellStyle(cs);

代码示例来源:origin: org.apache.poi/poi-examples

  1. /**
  2. * Inserts a single bulleted item into a cell.
  3. *
  4. * @param workbook A reference to the HSSFWorkbook that 'contains' the
  5. * cell.
  6. * @param listItem An instance of the String class encapsulating the
  7. * items text.
  8. * @param cell An instance of the HSSFCell class that encapsulates a
  9. * reference to the spreadsheet cell into which the list item
  10. * will be written.
  11. */
  12. public void bulletedItemInCell(HSSFWorkbook workbook, String listItem, HSSFCell cell) {
  13. // A format String must be built to ensure that the contents of the
  14. // cell appear as a bulleted item.
  15. HSSFDataFormat format = workbook.createDataFormat();
  16. String formatString = InCellLists.BULLET_CHARACTER + " @";
  17. int formatIndex = format.getFormat(formatString);
  18. // Construct an HSSFCellStyle and set it's data formt to use the
  19. // object created above.
  20. HSSFCellStyle bulletStyle = workbook.createCellStyle();
  21. bulletStyle.setDataFormat((short)formatIndex);
  22. // Set the cells contents and style.
  23. cell.setCellValue(new HSSFRichTextString(listItem));
  24. cell.setCellStyle(bulletStyle);
  25. }

代码示例来源:origin: com.haulmont.cuba/cuba-gui

  1. protected void createFormats() {
  2. timeFormatCellStyle = wb.createCellStyle();
  3. timeFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm"));
  4. dateFormatCellStyle = wb.createCellStyle();
  5. dateFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
  6. dateTimeFormatCellStyle = wb.createCellStyle();
  7. dateTimeFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
  8. integerFormatCellStyle = wb.createCellStyle();
  9. integerFormatCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
  10. DataFormat format = wb.createDataFormat();
  11. doubleFormatCellStyle = wb.createCellStyle();
  12. doubleFormatCellStyle.setDataFormat(format.getFormat("#,##0.################"));
  13. }

代码示例来源:origin: org.tentackle/tentackle-swing

  1. HSSFDataFormat format = wb.createDataFormat();

代码示例来源:origin: stackoverflow.com

  1. HSSFCellStyle dateStyle = workbook.createCellStyle();
  2. HSSFCellStyle datePatternStyle = workbook.createCellStyle();
  3. HSSFDataFormat format1 = workbook.createDataFormat();
  4. HSSFCellStyle dataStyle = workbook.createCellStyle();
  5. HSSFCellStyle numberStyle = workbook.createCellStyle();

相关文章

HSSFWorkbook类方法