java上传xls文件

using System;  

using System.Collections.Generic;  

using System.Web;  

using System.Web.UI;  

using System.Web.UI.WebControls;  

using PublicProject.ENTITY;  

using System.Data;  

using System.IO;  

using System.Data.OleDb;  

using PublicProject.BLL;  

public partial class 使用Excel读取数据 : System.Web.UI.Page  

{  

//集合  

private List<School> schoolList = new List<School>();  

private DataSet dsall = new DataSet();  

private int count = 0;  

private SchoolBll schoolBll = new SchoolBll();  

protected void Page_Load(object sender, EventArgs e)  

{  

}  

//public void ReaderXls()  

//{  

//    string xlsFilePath = @"F:\项目代码\公用架构\PublicProjectSolution\PublicProjectWeb\xls\DateReader.xls";  

//    Excel  

//}  

/// <summary>  

/// 单击导入按钮的事件  

/// </summary>  

/// <param name="sender"></param>  

/// <param name="e"></param>  

protected void BtnImport_Click1(object sender, EventArgs e)  

{  

string fileName = string.Empty;  

try 

{  

fileName= UpLoadXls(FileExcel);  

ImportXlsToData(fileName);//将XLS文件的数据导入数据库  

if (fileName != string.Empty && System.IO.File.Exists(fileName))  

{  

System.IO.File.Delete(fileName);//删除上传的XLS文件  

}  

LblMessage.Text = "数据导入成功!";  

this.gvimport.DataSource = schoolList;  

this.gvimport.DataBind();  

this.lbimport.Text = schoolList.Count.ToString();  

int all = Convert.ToInt32(this.lballcount.Text.ToString());  

int import = Convert.ToInt32(this.lbimport.Text.ToString());  

int unimport = all - import;  

if (unimport > count)  

{  

this.lbunimport.Text = "," + unimport.ToString() + "条,由于传输原因未导入到数据库!";  

}  

}  

catch (Exception ex)  

{  

LblMessage.Text=ex.Message;  

}  

}  

/// <summary>  

/// 上传Excel文件  

/// </summary>  

/// <param name="inputfile">上传的控件名</param>  

/// <returns></returns>  

private string UpLoadXls(System.Web.UI.HtmlControls.HtmlInputFile inputfile)  

{  

string orifilename = string.Empty;  

string uploadfilepath = string.Empty;  

string modifyfilename = string.Empty;  

string fileExtend = "";//文件扩展名  

int fileSize = 0;//文件大小  

try 

{  

if (inputfile.Value != string.Empty)  

{  

//得到文件的大小  

fileSize = inputfile.PostedFile.ContentLength;  

if (fileSize == 0)  

{  

throw new Exception("导入的Excel文件大小为0,请检查是否正确!");  

}  

//得到扩展名  

fileExtend = inputfile.Value.Substring(inputfile.Value.LastIndexOf(".") + 1);  

if (fileExtend.ToLower() != "xls")  

{  

throw new Exception("你选择的文件格式不正确,只能导入EXCEL文件!");  

}  

//路径  

uploadfilepath = Server.MapPath("~/xls/XlsUpLodes");  

//新文件名  

modifyfilename = System.Guid.NewGuid().ToString();  

modifyfilename += "." + inputfile.Value.Substring(inputfile.Value.LastIndexOf(".") + 1);  

//判断是否有该目录  

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadfilepath);  

if (!dir.Exists)  

{  

dir.Create();  

}  

orifilename = uploadfilepath + "\\" + modifyfilename;  

//如果存在,删除文件  

if (File.Exists(orifilename))  

{  

File.Delete(orifilename);  

}  

// 上传文件  

inputfile.PostedFile.SaveAs(orifilename);  

}  

else 

{  

throw new Exception("请选择要导入的Excel文件!");  

}  

}  

catch (Exception ex)  

{  

throw ex;  

}  

return orifilename;  

}  

/// <summary>  

/// 将上传文件中的数据读取到数据库中  

/// </summary>  

/// <param name="fileName">上传的文件的地址</param>  

private void ImportXlsToData(string fileName)  

{  

try 

{  

if (fileName == string.Empty)  

{  

throw new ArgumentNullException("Excel文件上传失败!");  

}  

string oleDBConnString = String.Empty;  

oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";  

oleDBConnString += "Data Source=";  

oleDBConnString += fileName;  

oleDBConnString += ";Extended Properties=Excel 8.0;";  

OleDbConnection oleDBConn = null;  

OleDbDataAdapter oleAdMaster = null;  

DataTable m_tableName = new DataTable();  

DataSet ds = new DataSet();  

oleDBConn = new OleDbConnection(oleDBConnString);  

oleDBConn.Open();  

m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  

if (m_tableName != null && m_tableName.Rows.Count > 0)  

{  

m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString();  

}  

string sqlMaster;  

sqlMaster = " SELECT *  FROM [" + m_tableName.TableName + "]";  

oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);  

oleAdMaster.Fill(ds, "m_tableName");  

oleAdMaster.Dispose();  

oleDBConn.Close();  

oleDBConn.Dispose();  

AddDatasetToSQL(ds, 8);  

}  

catch (Exception ex)  

{  

throw ex;  

}  

}  

/// <summary>  

/// 将Dataset的数据导入数据库  

/// </summary>  

/// <param name="pds">数据集</param>  

/// <param name="Cols">数据集列数</param>  

/// <returns></returns>  

private bool AddDatasetToSQL(DataSet pds, int Cols)  

{  

int ic, ir;  

ic = pds.Tables[0].Columns.Count;  

if (pds.Tables[0].Columns.Count < Cols)  

{  

throw new Exception("导入Excel格式错误!Excel只有" + ic.ToString() + "列");  

}  

ir = pds.Tables[0].Rows.Count;  

if (pds != null && pds.Tables[0].Rows.Count > 0)  

{  

for (int i = 0; i < pds.Tables[0].Rows.Count; i++)  

{  

School model = new School();  

string schooolid = pds.Tables[0].Rows[i][3].ToString();  

model.Email = pds.Tables[0].Rows[i][0].ToString();  

model.Pass= pds.Tables[0].Rows[i][1].ToString();  

model.RealName = pds.Tables[0].Rows[i][2].ToString();  

model.School1 = pds.Tables[0].Rows[i][3].ToString();  

model.ColSchool = pds.Tables[0].Rows[i][4].ToString();  

model.SubName = pds.Tables[0].Rows[i][5].ToString();  

model.GradeName = pds.Tables[0].Rows[i][6].ToString();  

model.ClassName = pds.Tables[0].Rows[i][7].ToString();  

//excel的格式:必须是英文列头  

//EMAIL 密码     姓名       学校    学院     专业     年级   班级  

//email  PASS  REAL_NAME SCHOOL  COL_NAME SUB_NAME GRADE  CLASS  

// 0   1      2        3       4       5        6      7  

schoolBll.Add(model);  

schoolList.Add(model);  

}  

//全部信息  

this.gvgetall.DataSource = schoolList;  

this.gvgetall.DataBind();  

dsall = pds;  

this.lballcount.Text = pds.Tables[0].Rows.Count.ToString();  

}  

else 

{  

throw new Exception("导入数据为空!");  

}  

return true;  

}  

/// <summary>  

/// 插入数据到数据库  

/// </summary>  

//public void Add(ccwu.Model.T_RESUME_INFO model)  

//{  

//    string sql = "select * from T_STUDENT_INFO where email=‘" + model.EMAIL.ToString() + "‘";//根据一个号去查询  

//    DataSet ds = ccwu.DBUtility.DbHelperSQL.Query(sql.ToString());  

//    //  int count = 0;  

//    if (ds.Tables[0].Rows.Count == 0)  

//    {  

//        //insert into PersonRecord  

//        ccwu.DAL.T_STUDENT_INFO dalstudent = new ccwu.DAL.T_STUDENT_INFO();  

//        ccwu.DAL.T_RESUME_INFO dalresume = new ccwu.DAL.T_RESUME_INFO();  

//        ccwu.Model.T_STUDENT_INFO m = new ccwu.Model.T_STUDENT_INFO();  

//        m.EMAIL = model.EMAIL.ToString();  

//        m.PASS = model.pass.ToString();  

//        m.SCHOOL_INFO_ID = Convert.ToInt32(model.SCHOOL_INFO_ID.ToString());  

//        //向学生注册表里  

//        int lastid = dalstudent.Add(m);  

//        model.STUDENT_INFO_ID = lastid;  

//        //向简历表里添加信息  

//        dalresume.AddForAll(model);  

//        list.Add(model);  

//    }  

//    if (ds.Tables[0].Rows.Count > 0)  

//    {  

//        count = count + 1;  

//    }  

//}  

protected void gvgetall_PageIndexChanging(object sender, GridViewPageEventArgs e)  

{  

}  

protected void gvimport_PageIndexChanging(object sender, GridViewPageEventArgs e)  

{  

}  

} 

java上传xls文件

时间: 2024-08-07 02:05:49

java上传xls文件的相关文章

edtftpj让Java上传FTP文件支持断点续传

在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一半,睡一觉去先,明天继续传. Java上传FTP文件,用的比较多的工具是apache的commons-net.如果想用commons-net实现FTP上传的断点续传还是有点麻烦. 除了commons-net之外,还有很多非常优秀的FTP工具,这里使用edtftpj这个工具来实现断点续传. 下载:ht

php上传xls文件导入到mysql数据表

申明下面的文章属于转载,现在大家新下载的版本跟下面列子这个不一样!我看了很多网上的列子,最后我还是读了那个英文的手册,上面说的很清楚!大家英语不好可以下载有道!其实重点是要包含下面两行: require_once 'excellib/PHPExcel.php'; require_once 'excellib/PHPExcel/IOFactory.php'; 你们各自的替换各自存放目录,我这里用的是excellib目录.就是要把PHPExcel.php',PHPExcel/IOFactory.ph

Java上传大文件

看了日志,错误为: 1 java.lang.OutOfMemoryError Java heap space 上传文件代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public static String uploadSingleFile(String path,MultipartFile file) {       if (!file.isEmpty()) {

文件上传---普通文件fileupload.jar和url文件httpUrlConnection

文件上传---普通文件和url文件 主要用来学习使用common-fileupload.jar和java.net.httpURLConnection 普通文件: 1 //上传xls文件到临时目录 2 if (! ServletFileUpload.isMultipartContent(request)) return; 3 DiskFileItemFactory factory = new DiskFileItemFactory(); // 建立factory 4 factory.setSize

文件上传---普通文件和url文件

主要用来学习使用common-fileupload.jar和java.net.httpURLConnection 普通文件: //上传xls文件到临时目录 if (! ServletFileUpload.isMultipartContent(request)) return; DiskFileItemFactory factory = new DiskFileItemFactory(); // 建立factory factory.setSizeThreshold(4*1024*1024); //

WebUploader插件上传大文件单文件和多文件JAVA版使用总结

一.使用webuploader插件的原因说明 被现在做的项目坑了. 先说一下我的项目架构Spring+struts2+mybatis+mysql 然后呢.之前说好的按照2G上传就可以了,于是乎,用了ajaxFileUpload插件,因为之前用图片上传也是用这个,所以上传附件的时候就直接拿来用了 各种码代码,测试也测过了,2G文件上传没问题,坑来了,项目上线后,客户又要求上传4G文件,甚至还有20G以上的..纳尼,你不早说哦... 在IE11下用ajaxFileUpload.js插件上传超过4G的

Java上传文件夹(Jersey)

背景介绍:公司要在CMS系统上为运营人员提供一个功能供运营人员将做好的活动页面上传到阿里云存储上,上传的内容为一个文件夹,文件夹内部有.html网页,JS文件夹下有JS文件,CSS文件夹下有样式表,Images文件夹下有多张图片,具体的目录接口如下: 要在网页上将整个文件夹进行上传,下面介绍下我整个实现的过程. 项目架构,前端使用的JSP,后端使用的Jersey,一个类似WebService的技术. 直接上代码,前端JSP文件: <%@ page language="java"

Java通过FTP服务器上传下载文件的解决方案

对于使用文件进行交换数据的应用来说,使用FTP 服务器是一个很不错的解决方案.本文使用Apache Jakarta Commons Net(commons-net-3.3.jar)基于FileZilla Server服务器实现FTP服务器上文件的上传/下载/删除等操作. 关于FileZilla Server服务器的详细搭建配置过程,详情请见FileZilla Server安装配置教程.之前有朋友说,上传大文件(几百M以上的文件)到FTP服务器时会重现无法重命名的问题,但本人亲测上传2G的文件到F

java web图片上传和文件上传

图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作. 注意事项 1.form表单一定要写属性enctype="multipart/form-data" 2.为了能保证文件能上传成功file控件的name属性值要和你提交的控制层变量名一致, 例如空间名是file那么你要在后台这样定义 private File file; //file控件名 private String fileContentType;//图