2016-08-31 32 views
-2

我想用csv轉換成xsl。一切工作正常,但在CSV文件中有一列有一些貨幣(如$ 400)。 當這些貨幣值寫入XLS文件時,它顯示綠色標誌,我們必須單擊並在Excel中將其數據類型從字符串更改爲數字以擺脫綠色標誌。

現在要做的是什麼是想檢查在一個單元格中,如果起始字符是'$',那麼我將setCelltype設置爲數字,但是要在代碼中實現它?

請幫助新的脫穎而出和Java太:P

//all imports are proper 
public class Convert_CSV_XLS { 
    public static void main(String[] args) throws Exception{ 

     /* Step -1 : Read input CSV file in Java */ 
     String inputCSVFile = "csv_2_xls.csv"; 
     CSVReader reader = new CSVReader(new FileReader(inputCSVFile)); 
     /* Variables to loop through the CSV File */ 
     String [] nextLine; /* for every line in the file */    
     int lnNum = 0; /* line number */ 
     /* Step -2 : Define POI Spreadsheet objects */   
     HSSFWorkbook new_workbook = new HSSFWorkbook(); //create a blank workbook object 
     HSSFSheet sheet = new_workbook.createSheet("CSV2XLS"); //create a worksheet with caption score_details 
     /* Step -3: Define logical Map to consume CSV file data into excel */ 
     Map<String, Object[]> excel_data = new HashMap<String, Object[]>(); //create a map and define data 
     /* Step -4: Populate data into logical Map */ 
     while ((nextLine = reader.readNext()) != null) { 
      lnNum++;       
      excel_data.put(Integer.toString(lnNum), new Object[] {nextLine[0],nextLine[1]});      
     } 
     /* Step -5: Create Excel Data from the map using POI */ 
     Set<String> keyset = excel_data.keySet(); 
     int rownum = 0; 
     for (String key : keyset) { //loop through the data and add them to the cell 
      Row row = sheet.createRow(rownum++); 
      Object [] objArr = excel_data.get(key); 
      int cellnum = 0; 
      for (Object obj : objArr) { 
       Cell cell = row.createCell(cellnum++); 

       // Now here i want to check if first char of the value in cell is '$' or not. 
       if(obj instanceof Double) 
        cell.setCellValue((Double)obj); 
       else 
        cell.setCellValue((String)obj); 
      } 
     } 
     /* Write XLS converted CSV file to the output file */ 
     FileOutputStream output_file = new FileOutputStream(new File("CSV2XLS.xls")); //create XLS file 
     new_workbook.write(output_file);//write converted XLS file to output stream 
     output_file.close(); //close the file 
    } 
    } 

回答

1

你應該能夠檢查,看是否有String開始與$或不漂亮容易。對於設定的細胞類型,它應該是這樣的,如果你想Numeric

cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); 
+0

請男人我想要代碼檢查單元格的第一個字符'$',幫我在這裏 我該怎麼辦? Array arr [] = getCellValue.tostring; arr [0] =='$'?? –

+1

這不是一個代碼寫入服務。我正在幫助您使用Excel代碼,因爲這不是「常見」的知識。在任何編程語言中檢查字符串中的字符是非常基本的考慮你想要做什麼。粗略的谷歌搜索會給你的代碼。顯示一些努力。 –

+0

cell.getCellValue會返回String中的值嗎?那麼我會這樣做 Array arr [] = getCellValue.tostring; arr [0] =='$'?? –

相關問題