• 大小: 2.42MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-15
  • 语言: Java
  • 标签: jxl  excel  java  

资源简介

jxl excel java excel导入数据库 java操作excel 数据库是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();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(1048576000);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(102100);
// 设置临时目录:
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 < sheet.getRows(); j++)// sheet.getRows()为excel数据的总行数
{
String username = sheet.getCell(0 j).getContents();// 获得第0列,第i行单元格中的数据
String password = sheet.getCell(1 j).getContents();
// 向数据库表中插入第i行数据的代码
String sql = “insert into user(username

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1108  2009-07-20 11:02  operateExcel\.classpath

     文件        309  2009-07-16 16:59  operateExcel\.mymetadata

     文件        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\meta-INF\MANIFEST.MF

     文件        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个文件信息

评论

共有 条评论