java - 使用Java将文本转换为xls中的数字

标签 java jakarta-ee apache-poi

当我通过java创建Excel工作表时,Oracle表中具有数字数据类型的列被转换为Excel中的文本格式。我希望它保持数字格式。下面是我创建Excel的代码片段。

    FileWriter fw = new FileWriter(tempFile.getAbsoluteFile(),true);
    //BufferedWriter bw = new BufferedWriter(fw);

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Excel Sheet");

    //Column Size of excel
    for(int i=0;i<10;i++)
    {
        sheet.setColumnWidth((short) i, (short)8000); 
    }

    String userSelectedValues=result;   

    HSSFCellStyle style = wb.createCellStyle();
    ///HSSFDataFormat df = wb.createDataFormat();

    style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    //style.setDataFormat(df.getFormat("0"));

    HSSFFont font = wb.createFont();
    font.setColor(HSSFColor.BLACK.index);
    font.setBoldweight((short) 700);
    style.setFont(font);

    int selecteditems=userSelectedValues.split(",").length;
    // HSSFRow rowhead = sheet.createRow((short)0);
    //System.out.println("**************selecteditems************" +selecteditems);

    for(int k=0; k<selecteditems;k++)
    {
        HSSFRow rowhead = sheet.createRow((short)k);

        if(userSelectedValues.contains("O_UID"))
        {   
          HSSFCell cell0 = rowhead.createCell((short) k);
          cell0.setCellValue("O UID");
          cell0.setCellStyle(style); 
          k=k+1;
        }   
    ///some columns here..
    }

    int index=1;
    for (int i = 0; i<dataBeanList.size(); i++) 
    {
        odb=(OppDataBean)dataBeanList.get(i);
        HSSFRow row = sheet.createRow((short)index);

        for(int j=0;j<selecteditems;j++)
        {
            if(userSelectedValues.contains("O_UID"))
            {            
                HSSFCell row1=row.createCell((short)j);
                row1.setCellType(row1.CELL_TYPE_NUMERIC);
                row1.setCellValue(odb.getUID());
                j=j+1;
            }
        }
        index++;
        }

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(path.toString()+"/temp.xls");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
     try {
        wb.write(fileOut);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        fileOut.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Excel screenshot

最佳答案

试试这个:

Cell cell = row.createCell(...);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html

在您的示例中,它将是:

Cell cell = row.createCell((short)j);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(odb.getUID());

关于java - 使用Java将文本转换为xls中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19515213/

相关文章:

java - Bean 管理事务和 org.hibernate.LazyInitializationException

java - 如何处理复合键 hibernate

java apache poi(第 1 部分)

java - POI 3.17 中的“HSSFCellStyle.BORDER_THIN”替代方案

excel - 从/xl/worksheets/sheet1.xml部分coldfusion poi中删除记录合并单元格

java - 为什么 RecyclerView 在其他 Kotlin Android 之前加载

java - 字节如何存储在整数中

java - 使用 OpenJDK 12 启动带有 Mockito 的 JUnit 时如何摆脱 "Could not initialize plugin: interface org.mockito.plugins.MockMaker"

java - 从文件中插入 200 万条记录到表中

json - 使用JEST将ElasticSearch结果映射到JPA实体