Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java将xls中的文本转换为数字_Java_Jakarta Ee_Apache Poi - Fatal编程技术网

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

使用Java将xls中的文本转换为数字,java,jakarta-ee,apache-poi,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(); HSSF

当我通过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();
    }
FileWriter fw=newfilewriter(tempFile.getAbsoluteFile(),true);
//BufferedWriter bw=新的BufferedWriter(fw);
HSSFWorkbook wb=新的HSSFWorkbook();
HSSFSheet sheet=wb.createSheet(“Excel工作表”);
//excel的列大小
对于(int i=0;i请尝试以下方法:

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

在您的示例中,它将是:

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

如何添加下面的代码?因为,我从数据库中获取bean形式的值。if(userSelectedValues.contains(“O_UID”){row.createCell((short)j)。setCellValue(odb.getUID());j=j+1;}嗨,我像这样添加了。hssfrowhead=sheet.createRow((short)k);if(userSelectedValues.contains(“O_UID”)){HSSFCell cell0=rowhead.createCell((短)k);cell0.setCellType(cell0.CELL_TYPE_NUMERIC);cell0.setCellValue(“0_UID”);cell0.setCellStyle(style);k=k+1;}这是标题部分。现在,如何在下面的代码中插入,因为我正在从数据库获取bean中的数据。odb=(OpportunityDataBean)dataGridBeanList.get(i);HSSFRow row=sheet.createRow((短)index);for(int j=0;jVery-similor,您只需要为单元格调用setCellType(…)方法,如用于标题。是否正确..我像这样添加了HSSFCell row0=row.createCell((短)j);row0.setCellType(cell0.cell_TYPE_NUMERIC);setCellValue(odb.getOpportunityUID());我按照u的建议添加了。但是在excel中,当我从列中选择值时,总和没有显示。在excel中它仍然被视为“文本”。请帮助。