2013-03-16 114 views
0

我是新来的Apache POI,我试图格式化一列单元格..我已经创建了电子表格创建值的地方..所有我要做的是从mm更改格式: ss.0到HH:MM:SS。Apache POI:如何格式化一列单元格?

如果任何人都可以对此提供一些见解,包括代码将非常感激。

所有帮助是值得欢迎的。

谢谢。

回答

2

org.apache.poi.ss.usermodel.DateUtil

convertTime(java.lang.String timeStr) 
      Converts a string of format "HH:MM" or "HH:MM:SS" to its (Excel) numeric equivalent 

应该解决您的需求。欲了解更多详情,请研究 - http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DateUtil.html

你也可以检查此下列程序是否能给你一些见解 -

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellStyle; 
import org.apache.poi.ss.usermodel.CreationHelper; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 

public class ExcelExample { 

    public static void main(String[] args) { 
     InputStream inp; 
     try { 
      inp = new FileInputStream("workbook.xls"); 

     Workbook wb = new HSSFWorkbook(inp); 
     CreationHelper createHelper = wb.getCreationHelper(); 

     Sheet sheet = wb.getSheetAt(0); 
     for (Row row : sheet) { 

      Cell cell = row.getCell(1); 
      CellStyle cellStyle = wb.createCellStyle(); 
      cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("hh:mm:ss")); 
      cell.setCellStyle(cellStyle); 

      } 

     // Write the output to a file 
     FileOutputStream fileOut = new FileOutputStream("workbook.xls"); 
     wb.write(fileOut); 
     fileOut.close(); 
     System.out.println("Done..."); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

} 

感谢

+0

谢谢您的迅速反应,我将如何靶细胞的一列并应用格式?可以说列是“A”,我想将格式应用到整个列。 – 2013-03-16 15:47:24

+0

我编辑了我的答案...您可以检查它... – 2013-03-16 16:43:49

+0

谢谢!我已标记为正确 – 2013-03-16 16:49:28