从Excel读数据的问题
1.我用POI从Excel读取日期数据,月份为什么是00?即Excel中是2006-4-11,显示出来的是2006-00-11
代码如下:
ChangeDateFormat.java
---------------------------------------
package changeDate;
import java.text.*;
import java.util.Date;
public class ChangeDateFormat {
public String ChangeToYYMMDD(Date fullDate){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String date=sdf.format(fullDate);
return date;
}
}
-------------------------------------------
showtable.jsp
-------------------------------------------
<%@ page contentType="text/html;charset=GBK"%>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<%@ page import="java.io.*,changeDate.ChangeDateFormat,org.apache.poi.hssf.usermodel.*"%>
<table align="center" border=1>
<%
//读取.xls
String filename=request.getParameter("filename");
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
// 创建对工作表的引用。
// 本例是按名引用,
HSSFSheet sheet = workbook.getSheetAt(0);
// 也可用getSheetAt(int index)按索引引用,
// 在Excel文档中,第一张工作表的缺省索引是0,
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
int i=0;
// 读取一行
HSSFRow row = sheet.getRow(i);
HSSFCell cell;
ChangeDateFormat cdf=new ChangeDateFormat();
while(row!=null){
%>
<tr>
<%
cell = row.getCell((short)0);
java.util.Date time=cell.getDateCellValue();
String timestr=cdf.ChangeToYYMMDD(time);
%>
<td align="center"><%=timestr%></td>
<%cell = row.getCell((short)1);
%>
<td align="center"><%=cell.getNumericCellValue()%></td>
<%
cell = row.getCell((short)2);
%>
<td align="center"><%=cell.getStringCellValue()%></td>
<%
cell = row.getCell((short)3);
%>
<td align="center">1</td>
<%
cell = row.getCell((short)4);
%>
<td align="center">1</td>
<%
cell = row.getCell((short)5);
%>
<td align="center">1</td>
<%
cell = row.getCell((short)6);
%>
<td align="center">1</td>
<%
cell = row.getCell((short)7);
%>
<td align="center">1</td>
<%
cell = row.getCell((short)8);
%>
<td align="center">1</td>
</tr>
<%
i=i+1;
row=sheet.getRow(i);
}
%>
</table>