110,537
社区成员
发帖
与我相关
我的任务
分享
//读取Excel数据
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + fileName + ";Extended Properties='Excel "
+ (fileName.EndsWith("xls", StringComparison.CurrentCultureIgnoreCase) ? "8" : "12") + ".0;HDR=Yes'";
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
//返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
//包含excel中表名的字符串数组
for (int k = 0; k < dtSheetName.Rows.Count; k++)
{
string strTableName = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
string str = "select * from [" + strTableName + "]";
OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
da.Fill(ds, strTableName.Replace("$", "").ToLower());
da.Dispose();
}
conn.Close();
conn.Dispose();
}
return ds;
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="TableName"></param>
/// <param name="tableName2">如果这个有就以他为表名,没有的话就以TableName</param>
private DataTable ImportExcel(string Path, string TableName, string tableName2)
{
DataTable dt = new DataTable();
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter mycommand = null;
if (tableName2.Length > 0 && !tableName2.Equals(string.Empty))
TableName = tableName2;
strExcel = "select * from [" + TableName + "$]";
mycommand = new OleDbDataAdapter(strExcel, conn);
mycommand.Fill(dt);
conn.Close();
}
catch (Exception ex)
{
Common.MessageBox.Show(this.Page, "导入失败!原因:" + ex.Message + "");
}
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = "";
if (FileUpload2.HasFile)
{
try
{
DataTable dt = new DataTable();
int len = this.FileUpload2.FileName.ToString().Length;
path = this.FileUpload2.FileName.ToString().Trim();
path = Server.MapPath("~/Upload/" + path);
this.FileUpload2.SaveAs(path);
dt = ImportExcel(path, this.FileUpload2.FileName.ToString().Substring(0, len - 4), this.tbxExcelName.Text);
if (Session["dt"] != null)
Session.Remove("dt");
Session.Add("dt", dt);
List<Model.student_user> list = new List<Model.student_user>();
string msg = string.Empty;
int num = 0;
int i = 0;
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{}
}
}
catch (Exception ex)
{
Common.MessageBox.Show(this.Page, "导入失败!原因:" + ex.Message + "");
}
}
else
{
Common.MessageBox.Show(this.Page, "选择Excel路径");
return;
}
deleteFile(path);
}
/// <summary>
/// 读取Excel文档
/// </summary>
/// <param name="xPath">文件名称</param>
/// <param name="sheetIndex">sheet序号</param>
/// <returns>返回一个数据集</returns>
public static DataSet readExcel(string xPath, string sheetIndex)
{
Microsoft.Office.Interop.Excel.Application ExcelRS = new ApplicationClass();
Workbook RSbook = ExcelRS.Workbooks.Open(xPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Worksheet RSsheet = (Worksheet)RSbook.Sheets.get_Item(sheetIndex);
string sheetName = RSsheet.Name;
RSbook.Close();
RSsheet = null;
RSbook = null;
ExcelRS = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers(); string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + xPath + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
try
{
conn.Open();
string strExcel = string.Format("select * from [{0}$]", sheetName);
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet ds = new DataSet();
myCommand.Fill(ds);
return ds;
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 以Excel为数据源获取数据集
/// </summary>
/// <returns></returns>
private DataSet createDataSource()
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/Files/Employee.xls") + ";Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(strCon);
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Employee$]", con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}