jxlでレポートExcelデータをインポートし、excelが日付形式の場合は8時間減算する必要があります
2577 ワード
<span style="font-size:18px;"> jxl Excel , excel 8
package cn.itcast.elec.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
public class GenerateSqlFromExcel {
/**
* Excel ,
* @param formFile
* @return list ArrayList
* @throws Exception
*/
public static ArrayList generateStationBugSql(File formFile)
throws Exception {
InputStream in = null;
Workbook wb = null;
ArrayList list = new ArrayList();
try {
if (formFile == null) {
throw new Exception(" !");
}
in = new FileInputStream(formFile);
wb = Workbook.getWorkbook(in);
Sheet sheet[] = wb.getSheets();
if (sheet != null) {
for (int i = 0; i < sheet.length; i++) {
if (!sheet[i].getName().equalsIgnoreCase("User")) {
throw new Exception(" User sheet, !");
}
for (int j = 1; j < sheet[i].getRows(); j++) {
String[] valStr = new String[8];
for (int k = 0; k < sheet[i].getColumns(); k++) {
Cell cell = sheet[i].getCell(k, j);
String content = "";
if (cell.getType() == CellType.DATE) {
<strong>DateCell dateCell = (DateCell) cell;
java.util.Date importdate = dateCell.getDate();</strong></span>
<span style="font-size:18px;"><strong><span style="white-space:pre"> </span>/** excel 8 */
long eighthour = 8*60*60*1000;
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/** 8 */
long time = importdate.getTime()-eighthour;
java.util.Date date = new java.util.Date();
date.setTime(time);
content = simpledate.format(date);</strong>
} else {
String tempcontent = (cell.getContents() == null ? ""
: cell.getContents());
content = tempcontent.trim().replace('\'', ' ');
}
valStr[k] = content;
}
list.add(j-1,valStr);
}
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (wb != null) {
wb.close();
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
</span>