用 java 读取excel 文件里的时间格式数据时,24时制会自动转化为12时制,解决办法

在用java 读取excel文件时会有这样的情况的发生 文件内容 18:05 读出后 6:05 原因是引用jxl 时有个pattern类用的是12时制的时间,所以...., 要自己写个方法来再次将它读出的时间进行转换.

 

package  blogic;



import  jxl.Cell;
import  jxl.DateCell;
import  jxl.Sheet;
import  jxl.Workbook;
import  jxl.read.biff.BiffException;
import  java.io.IOException;
import  java.io.InputStream;
import  java.text.SimpleDateFormat;
import  java.io.FileInputStream;
import  java.io.File;

/**
 * <概要描述> 倒入考勤表
 * 
 * 
@author xx
 * 
@version 1.0 2008/01/07
 
*/

public   class  readExcel  {
    
/**
     * <概要描述> excle文件中时间类型数据格式转换
     * 
     * 
@param excle文件中时间类型数据
     * 
@return String
     * 
@throws 无
     
*/

    
public static String FormateTime(Cell formatecell) {
        
try {
            java.util.Date mydate 
= null;
            DateCell datecll 
= (DateCell) formatecell;
            mydate 
= datecll.getDate();
            
long time = (mydate.getTime() / 1000- 60 * 60 * 8;
            mydate.setTime(time 
* 1000);
            SimpleDateFormat formatter 
= new SimpleDateFormat("HH:mm");
            
return formatter.format(mydate);
        }
 catch (Exception e) {
            e.printStackTrace();
            
return null;
        }

    }


    
public static void main(String args[]){
        readExcel readexcel
=new readExcel();
        String filePath
="C:/modual.xls";
        readexcel.ReadExcelToDb(filePath);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以使用 Apache POI 库来读取 Excel 文件,并将其转换为 HTML 格式。以下是一个简单的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFDateUtil; public class ExcelToHtmlConverter { public static void main(String[] args) throws IOException { String excelFilePath = "path/to/excel/file"; Workbook workbook = new HSSFWorkbook(excelFilePath); Sheet sheet = workbook.getSheetAt(0); StringBuilder html = new StringBuilder("<table>"); for (Row row : sheet) { html.append("<tr>"); for (Cell cell : row) { html.append("<td>"); switch (cell.getCellType()) { case STRING: html.append(cell.getRichStringCellValue() .getString()); break; case NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { html.append(cell.getDateCellValue()); } else { html.append(cell.getNumericCellValue()); } break; case BOOLEAN: html.append(cell.getBooleanCellValue()); break; case FORMULA: html.append(cell.getCellFormula()); break; default: html.append(""); } html.append("</td>"); } html.append("</tr>"); } html.append("</table>"); FileOutputStream fos = new FileOutputStream("path/to/html/file"); fos.write(html.toString().getBytes()); fos.close(); } } ``` 这个代码将读取 Excel 文件的第一个工作表,并将其转换为 HTML 表格,然后将结果写入一个文件。你需要将 `path/to/excel/file` 和 `path/to/html/file` 替换为你的实际路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值