資源簡介
jxl excel java excel導(dǎo)入數(shù)據(jù)庫 java操作excel
數(shù)據(jù)庫是mysql

代碼片段和文件信息
/*
?*?Generated?by?MyEclipse?Struts
?*?Template?path:?templates/java/JavaClass.vtl
?*/
package?com.eastman.struts.action;
import?java.io.File;
import?java.io.FileInputStream;
import?java.sql.Connection;
import?java.sql.Statement;
import?java.text.ParseException;
import?java.text.SimpleDateFormat;
import?java.util.Date;
import?java.util.Iterator;
import?java.util.List;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
import?jxl.Cell;
import?jxl.CellType;
import?jxl.DateCell;
import?jxl.Sheet;
import?jxl.Workbook;
import?org.apache.commons.fileupload.DiskFileUpload;
import?org.apache.commons.fileupload.FileItem;
import?org.apache.struts.action.ActionForm;
import?org.apache.struts.action.ActionForward;
import?org.apache.struts.action.ActionMapping;
import?org.apache.struts.actions.DispatchAction;
import?com.eastman.util.DBConnection;
import?com.eastman.util.FileUtil;
/**?
?*/
public?class?ImportExcelAction?extends?DispatchAction
{
public?ActionForward?importExcel(ActionMapping?mapping?ActionForm?form?HttpServletRequest?request
HttpServletResponse?response)?{
Connection?con?=?null;
Statement?sta?=?null;
String?target?=?““;
try?{
//?用于存放上傳文件的目錄
String?uploadPath?=?FileUtil.getUpLoadPath();
File?path?=?new?File(uploadPath);
if?(!path.exists())?{
path.mkdir();
}
String?tempPath?=?uploadPath;?//?用于存放臨時文件的目錄
DiskFileUpload?fu?=?new?DiskFileUpload();
//?設(shè)置最大文件尺寸,這里是4MB
fu.setSizeMax(1048576000);
//?設(shè)置緩沖區(qū)大小,這里是4kb
fu.setSizeThreshold(102100);
//?設(shè)置臨時目錄:
fu.setRepositoryPath(tempPath);
//?得到所有的文件:
List?fileItems?=?fu.parseRequest(request);
Iterator?i?=?fileItems.iterator();
String?fileName?=?null;
//?依次處理每一個文件:
while?(i.hasNext())?{
FileItem?fi?=?(FileItem)?i.next();
//?獲得文件名,這個文件名包括路徑:
fileName?=?fi.getName();
if?(fileName?!=?null)?{
//?寫入文件
int?lastindex?=?0;
lastindex?=?fileName.lastIndexOf(“\\“)?+?1;
if?(lastindex?<=?0)?{
lastindex?=?fileName.lastIndexOf(“/“)?+?1;
}
//?System.out.println(“?lastindex?/“?+lastindex);
fileName?=?fileName.substring(lastindex);
//?System.out.println(“fileName:“+fileName);
fileName?=?new?String(fileName.getBytes(“gbk“)?“gbk“);
//?System.out.println(“fileName:“+fileName);
fi.write(new?File(uploadPath?+?“/“?+?fileName));
}
}
File?file?=?new?File(uploadPath?+?“/“?+?fileName);
FileInputStream?is?=?new?FileInputStream(file);
Workbook?workbook?=?Workbook.getWorkbook(is);//?excel文檔
Sheet?sheet?=?workbook.getSheet(0);//?excel的sheet
for?(int?j?=?1;?j? {
String?username?=?sheet.getCell(0?j).getContents();//?獲得第0列,第i行單元格中的數(shù)據(jù)
String?password?=?sheet.getCell(1?j).getContents();
//?向數(shù)據(jù)庫表中插入第i行數(shù)據(jù)的代碼
String?sql?=?“insert?into?user(username
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1108??2009-07-20?11:02??operateExcel\.classpath
?????文件????????309??2009-07-16?16:59??operateExcel\.myme
?????文件????????258??2009-07-16?17:42??operateExcel\.mystrutsdata
?????文件???????1313??2009-07-17?08:57??operateExcel\.project
?????文件?????????86??2009-07-17?16:52??operateExcel\.settings\org.eclipse.core.resources.prefs
?????文件????????151??2009-07-16?17:51??operateExcel\src\ApplicationResources.properties
?????文件???????4756??2009-07-20?11:06??operateExcel\src\com\eastman\struts\action\ImportExcelAction.java
?????文件???????1821??2009-07-20?11:00??operateExcel\src\com\eastman\test\Test.java
?????文件????????631??2009-07-17?17:10??operateExcel\src\com\eastman\util\DBConnection.java
?????文件????????963??2009-07-20?10:59??operateExcel\src\com\eastman\util\FileUtil.java
?????文件????????238??2009-07-20?11:05??operateExcel\WebRoot\error.jsp
?????文件????????368??2009-07-17?16:54??operateExcel\WebRoot\index.jsp
?????文件?????????39??2009-07-16?16:59??operateExcel\WebRoot\me
?????文件????????242??2009-07-20?11:04??operateExcel\WebRoot\success.jsp
?????文件????????778??2009-07-20?11:07??operateExcel\WebRoot\WEB-INF\.struts-config.mex
?????文件????????151??2009-07-16?17:51??operateExcel\WebRoot\WEB-INF\classes\ApplicationResources.properties
?????文件???????5424??2009-07-20?11:06??operateExcel\WebRoot\WEB-INF\classes\com\eastman\struts\action\ImportExcelAction.class
?????文件???????2484??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\classes\com\eastman\test\Test.class
?????文件???????1139??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\classes\com\eastman\util\DBConnection.class
?????文件???????1545??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\classes\com\eastman\util\FileUtil.class
?????文件?????352244??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\antlr.jar
?????文件?????186642??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\commons-beanutils.jar
?????文件?????167311??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\commons-digester.jar
?????文件??????22484??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\commons-fileupload.jar
?????文件??????37960??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\commons-logging.jar
?????文件??????84151??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\commons-validator.jar
?????文件??????64870??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\jakarta-oro.jar
?????文件?????743361??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\jxl.jar
?????文件?????536609??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\mysql-connector-java-5.0.7-bin.jar
?????文件?????549830??2009-07-20?11:02??operateExcel\WebRoot\WEB-INF\lib\struts.jar
............此處省略31個文件信息
評論
共有 條評論