导入excel 请教一下请问怎么导入导出excel,最好写一个完整的实例给我,谢谢啦! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 打开IE,在地址栏输入Google,然后输入java操作excel,保证有你想要的 如果是操作excel,可以使用POI。同样的,导入导出excel也可以采用POI先把excel读入内存,然后进行其他操作,如向数据库插入等导入导出excel,是向数据库中导入吗?如果是的话,不同的数据库用比较高效的方式,如oracle,在程序中可调用其组件sql loader进行excel数据加载。 public final Vector<WorkExcelRowField> getExcelItem(final String strPath) throws Exception{ try { String strCardID = ""; String strDate = ""; String strStartTime = ""; String strEndTime = ""; String strTime = ""; String[] strTimes = null; String strWorkingTime = ""; String strLateCount = ""; int intLastRowNum; // get Excel Book HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream( strPath)); // get Excel sheet HSSFSheet sheet = workbook.getSheetAt(0); // get excel rows count intLastRowNum = sheet.getLastRowNum(); // dim a excel row HSSFRow row; // dim a excel cell HSSFCell cell; // get user info getUserPrioInfo(); // get row at 0 row = sheet.getRow(0); getUserIDInfo(); logger.debug("intLastRowNum = " + intLastRowNum); // read Excel data for (int i = 1; i < intLastRowNum + 1; i++) { // get row row = sheet.getRow(i); // get Cardid cell = row.getCell((int) 0); if (cell == null) continue; if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { strCardID = cell.getRichStringCellValue().toString(); } logger.debug("strCardID = " + strCardID); // get date cell = row.getCell((int) 3); if (cell == null) continue; if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { strDate = cell.getRichStringCellValue().toString(); strDate = formatData(strDate); } logger.debug("strDate = " + strDate); // get times cell = row.getCell((int) 4); if (cell == null) { strTimes = new String[] {}; } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { strTime = cell.getRichStringCellValue().toString(); strTimes = strTime.split(" "); } logger.debug("strTime = " + strTime); if (strTimes == null) { vecExcelData.addElement(new WorkExcelRowField(strCardID, strDate, "", "", "", "")); continue; } // if over two time if (strTimes.length > 1) { // get start time strStartTime = strTimes[0]; // get end time strEndTime = strTimes[strTimes.length - 1]; logger.debug(strStartTime); logger.debug(strEndTime); // get working time strWorkingTime = getWorkingTime(strStartTime, strEndTime, strCardID, strDate); logger.debug("working time " + strWorkingTime); // get late times strLateCount = String.valueOf(mapLateInfo.get(mapIDInfo .get(strCardID) + "," + getMonth(strDate))); logger.debug("work time = " + strWorkingTime.trim()); if (strCardID.trim() != "" && strDate.trim() != "" && strStartTime.trim() != "" && strEndTime.trim() != "" && strWorkingTime.trim() != "" && strLateCount.trim() != "") { // import data vecExcelData.addElement(new WorkExcelRowField( strCardID, strDate, strStartTime, strEndTime, strWorkingTime, strLateCount)); } strCardID = ""; strDate = ""; strStartTime = ""; strEndTime = ""; strTime = ""; strTimes = null; strWorkingTime = ""; strLateCount = ""; } else { if (strCardID.trim() != "" && strDate.trim() != "") { // if have no time or have only one time vecExcelData.addElement(new WorkExcelRowField( strCardID, strDate, "", "", "", "")); } strCardID = ""; strDate = ""; strStartTime = ""; strEndTime = ""; strTime = ""; strTimes = null; strWorkingTime = ""; strLateCount = ""; } } } catch (Exception e) { throw e; } return vecExcelData; }这只是写好的部分代码 我的资源里分享了个 jsp 导出 excel 源码,还有个 java 解析 execl 至 jsp 源码、java 版 excel 导入 数据库源码 楼主去看看,参考下 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>EB页面导出为EXCEL文档的方法</title> <script type="text/javascript"> <!-- downloadfunction saveCode(obj) { var winname = window.open('', '_blank', 'top=10000'); var strHTML = document.all.tableExcel.innerHTML; winname.document.open('text/html', 'replace'); winname.document.writeln(strHTML); winname.document.execCommand('saveas','','excel.xls'); winname.close(); } --> </script> </head> <body> <div id="tableExcel"> <table id="test" width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td colspan="5" align="center">WEB页面导出为EXCEL文档的方法</td> </tr> <tr> <td>列标题1</td> <td>列标题2</td> <td>列标题3</td> <td>列标题4</td> <td>列标题5</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ccc</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>AAA</td> <td>BBB</td> <td>CCC</td> <td>DDD</td> <td>EEE</td> </tr> <tr> <td>FFF</td> <td>GGG</td> <td>HHH</td> <td>III</td> <td>JJJ</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ccc</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>AAA</td> <td>BBB</td> <td>CCC</td> <td>DDD</td> <td>EEE</td> </tr> <tr> <td colspan="5">FFFGGGHHHIIIJJJ</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ccc</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>AAA</td> <td>BBB</td> <td rowspan="4">CCCHHHcccccc</td> <td>DDD</td> <td>EEE</td> </tr> <tr> <td>FFF</td> <td>GGG</td> <td>III</td> <td>JJJ</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>aaa</td> <td>bbb</td> <td>ddd</td> <td>eee</td> </tr> <tr> <td>AAA</td> <td>BBB</td> <td>CCC</td> <td>DDD</td> <td>EEE</td> </tr> <tr> <td>FFF</td> <td>GGG</td> <td>HHH</td> <td>III</td> <td>JJJ</td> </tr> </table> </div> <input type="button" value="另存为 Excel" onclick="saveCode(tableExcel)"> </body> </html> js 脚本下载问题 Extjs stroe更新问题 关于html的一个问题?? 大家帮我看看错误吧,急,,谢谢啦! 问一下jsp分页显示的方法!!!!! 数据库里的时间类型的数据在jsp显示 Resin的问题! 在线等!!!swf中的超链接路径找不到 查了很多以前的帖子,还是没配置成自己的虚拟目录!请指点1,2 感谢送分!tomcat jsp win2000 Professional下如何配置JSP的环境 正则表达式,高手进来帮忙写一个 JAVA 运行通过的 程序,放到 JSP中的怪问题
如果是操作excel,可以使用POI。
同样的,导入导出excel也可以采用POI先把excel读入内存,然后进行其他操作,如向数据库插入等
导入导出excel,是向数据库中导入吗?如果是的话,不同的数据库用比较高效的方式,如oracle,在程序中可调用其组件sql loader进行excel数据加载。
String strCardID = "";
String strDate = "";
String strStartTime = "";
String strEndTime = "";
String strTime = "";
String[] strTimes = null;
String strWorkingTime = "";
String strLateCount = "";
int intLastRowNum;
// get Excel Book
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
strPath));
// get Excel sheet
HSSFSheet sheet = workbook.getSheetAt(0);
// get excel rows count
intLastRowNum = sheet.getLastRowNum();
// dim a excel row
HSSFRow row;
// dim a excel cell
HSSFCell cell;
// get user info
getUserPrioInfo();
// get row at 0
row = sheet.getRow(0);
getUserIDInfo(); logger.debug("intLastRowNum = " + intLastRowNum);
// read Excel data
for (int i = 1; i < intLastRowNum + 1; i++) {
// get row
row = sheet.getRow(i);
// get Cardid
cell = row.getCell((int) 0);
if (cell == null)
continue;
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
strCardID = cell.getRichStringCellValue().toString();
}
logger.debug("strCardID = " + strCardID);
// get date
cell = row.getCell((int) 3);
if (cell == null)
continue;
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
strDate = cell.getRichStringCellValue().toString();
strDate = formatData(strDate);
}
logger.debug("strDate = " + strDate);
// get times
cell = row.getCell((int) 4);
if (cell == null) {
strTimes = new String[] {};
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
strTime = cell.getRichStringCellValue().toString();
strTimes = strTime.split(" ");
}
logger.debug("strTime = " + strTime);
if (strTimes == null) {
vecExcelData.addElement(new WorkExcelRowField(strCardID,
strDate, "", "", "", ""));
continue;
}
// if over two time
if (strTimes.length > 1) { // get start time
strStartTime = strTimes[0]; // get end time
strEndTime = strTimes[strTimes.length - 1];
logger.debug(strStartTime);
logger.debug(strEndTime); // get working time
strWorkingTime = getWorkingTime(strStartTime, strEndTime,
strCardID, strDate);
logger.debug("working time " + strWorkingTime); // get late times
strLateCount = String.valueOf(mapLateInfo.get(mapIDInfo
.get(strCardID)
+ "," + getMonth(strDate)));
logger.debug("work time = " + strWorkingTime.trim());
if (strCardID.trim() != "" && strDate.trim() != ""
&& strStartTime.trim() != ""
&& strEndTime.trim() != ""
&& strWorkingTime.trim() != ""
&& strLateCount.trim() != "") {
// import data
vecExcelData.addElement(new WorkExcelRowField(
strCardID, strDate, strStartTime, strEndTime,
strWorkingTime, strLateCount));
}
strCardID = "";
strDate = "";
strStartTime = "";
strEndTime = "";
strTime = "";
strTimes = null;
strWorkingTime = "";
strLateCount = "";
} else {
if (strCardID.trim() != "" && strDate.trim() != "") {
// if have no time or have only one time
vecExcelData.addElement(new WorkExcelRowField(
strCardID, strDate, "", "", "", ""));
}
strCardID = "";
strDate = "";
strStartTime = "";
strEndTime = "";
strTime = "";
strTimes = null;
strWorkingTime = "";
strLateCount = "";
}
} } catch (Exception e) {
throw e;
}
return vecExcelData;
}这只是写好的部分代码
楼主去看看,参考下
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>EB页面导出为EXCEL文档的方法</title>
<script type="text/javascript">
<!-- download
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
var strHTML = document.all.tableExcel.innerHTML;
winname.document.open('text/html', 'replace');
winname.document.writeln(strHTML);
winname.document.execCommand('saveas','','excel.xls');
winname.close();
}
-->
</script>
</head> <body>
<div id="tableExcel">
<table id="test" width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5" align="center">WEB页面导出为EXCEL文档的方法</td>
</tr>
<tr>
<td>列标题1</td>
<td>列标题2</td>
<td>列标题3</td>
<td>列标题4</td>
<td>列标题5</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
<td>DDD</td>
<td>EEE</td>
</tr>
<tr>
<td>FFF</td>
<td>GGG</td>
<td>HHH</td>
<td>III</td>
<td>JJJ</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
<td>DDD</td>
<td>EEE</td>
</tr>
<tr>
<td colspan="5">FFFGGGHHHIIIJJJ</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>AAA</td>
<td>BBB</td>
<td rowspan="4">CCCHHHcccccc</td>
<td>DDD</td>
<td>EEE</td>
</tr>
<tr>
<td>FFF</td>
<td>GGG</td>
<td>III</td>
<td>JJJ</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
<td>DDD</td>
<td>EEE</td>
</tr>
<tr>
<td>FFF</td>
<td>GGG</td>
<td>HHH</td>
<td>III</td>
<td>JJJ</td>
</tr>
</table>
</div>
<input type="button" value="另存为 Excel" onclick="saveCode(tableExcel)">
</body>
</html>