0

我是 Apache POI 的新手,我正在尝试格式化一列单元格。我已经用适当的值创建了电子表格。我要做的就是从 mm:ss 更改格式。 0 到 HH:MM:SS。

如果有人能对此提供一些见解,包括代码,将不胜感激。

欢迎所有帮助。

谢谢你。

4

1 回答 1

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

should solve your requirement. For more details please study - http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DateUtil.html

Also you can check whether this following program give you some insight -

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();
        }

    }

}

Thanks

于 2013-03-16T15:26:21.120 回答