资源简介
一个上传Excel文件并将内容保存到数据库中,附有完整代码,希望对您有帮助.
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender EventArgs e)
{
}
protected void Button1_Click(object sender EventArgs e)
{
String FR_file_server = @“Files/“;
String FR_mpath = Server.MapPath(FR_file_server);
String FR_filepath = fupImport.PostedFile.FileName;
String FR_mfilename = FR_filepath.Substring(FR_filepath.LastIndexOf(“.“) + 1);
string importTime = DateTime.Now.ToShortDateString() + “-“ + DateTime.Now.ToString(“hh-mm-ss“);
string FR_newFailureReportFileName = “Import“ + “.“ + FR_mfilename;
fupImport.PostedFile.SaveAs(FR_mpath + FR_newFailureReportFileName);
FR_filepath = FR_mpath + FR_newFailureReportFileName;
Session[“filepath“] = FR_mpath + FR_newFailureReportFileName;
int b = FR_filepath.LastIndexOf(@“.“);
string postfix_Str = FR_filepath.Substring(b + 1 FR_filepath.Length - b - 1);
if (postfix_Str.ToUpper() == “XLS“ || postfix_Str.ToUpper() == “XLSX“)
{
Session[“filepath“] = FR_filepath;
ExcelToDataTable(FR_filepath);
}
else
{
Page.Clientscript.RegisterStartupscript(this.GetType() ““ “ript>alert(‘The file format is not incorrect !‘) ript>“);
return;
}
GC.Collect();
}
protected void Button2_Click(object sender EventArgs e)
{
gridbind();
}
public DataTable getExcelSchemaByFileName(String fileName)
{
String sConnectionString = ““;
int b = fileName.LastIndexOf(@“.“);
string postfix_Str = fileName.Substring(b + 1 fileName.Length - b - 1);
if (postfix_Str.ToUpper() == “XLS“)
{
sConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;“ + “Data Source=“ + fileName + “;“ + “Extended Properties=Excel 8.0;“;
}
else if (postfix_Str.ToUpper() == “XLSX“)
{
sConnectionString = @“Provider=Microsoft.ACE.OLEDB.12.0;“ + @“Data Source=“ + fileName + “;“ + “Extended Properties=\“Excel 12.0 xml;\““;
}
OleDbConnection conn = new OleDbConnection(sConnectionString);
conn.Open();
DataTable dt_Excel = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables null);
conn.Close();
return dt_Excel;
}
public bool ExcelToDataTable(string strPath)
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2011-12-21 09:09 WebApplication1\WebApplication1\App_Data
文件 1093632 2010-03-19 11:31 WebApplication1\WebApplication1\bin\Excel.dll
文件 64088 2007-10-10 09:48 WebApplication1\WebApplication1\bin\Microsoft.Vbe.Interop.dll
文件 223800 2007-10-10 09:48 WebApplication1\WebApplication1\bin\office.dll
文件 10240 2011-12-21 10:02 WebApplication1\WebApplication1\bin\WebApplication1.dll
文件 22016 2011-12-21 10:02 WebApplication1\WebApplication1\bin\WebApplication1.pdb
目录 0 2011-12-21 09:44 WebApplication1\WebApplication1\bin
文件 2243 2011-12-21 09:55 WebApplication1\WebApplication1\Default.aspx
文件 5688 2011-12-21 10:00 WebApplication1\WebApplication1\Default.aspx.cs
文件 2903 2011-12-21 09:42 WebApplication1\WebApplication1\Default.aspx.designer.cs
文件 0 2011-12-21 09:56 WebApplication1\WebApplication1\Files\Import
文件 237056 2011-12-21 10:00 WebApplication1\WebApplication1\Files\Import.xls
文件 81977 2011-12-21 10:00 WebApplication1\WebApplication1\Files\Import.xlsx
目录 0 2011-12-21 09:56 WebApplication1\WebApplication1\Files
文件 237056 2011-12-09 09:54 WebApplication1\WebApplication1\loading01.xls
文件 81977 2011-12-21 09:29 WebApplication1\WebApplication1\loading02.xlsx
文件 12109 2011-12-21 09:47 WebApplication1\WebApplication1\obj\Debug\ResolveAssemblyReference.cache
目录 0 2011-12-21 09:09 WebApplication1\WebApplication1\obj\Debug\TempPE
文件 936 2011-12-21 09:44 WebApplication1\WebApplication1\obj\Debug\WebApplication1.csproj.FileListAbsolute.txt
文件 10240 2011-12-21 10:02 WebApplication1\WebApplication1\obj\Debug\WebApplication1.dll
文件 22016 2011-12-21 10:02 WebApplication1\WebApplication1\obj\Debug\WebApplication1.pdb
目录 0 2011-12-21 10:02 WebApplication1\WebApplication1\obj\Debug
目录 0 2011-12-21 09:09 WebApplication1\WebApplication1\obj
文件 1317 2011-12-21 09:09 WebApplication1\WebApplication1\Properties\AssemblyInfo.cs
目录 0 2011-12-21 09:09 WebApplication1\WebApplication1\Properties
文件 358 2011-12-21 10:02 WebApplication1\WebApplication1\ReadMe.txt
文件 3890 2011-12-21 09:44 WebApplication1\WebApplication1\SqlDataHelper.cs
文件 7436 2011-12-21 10:00 WebApplication1\WebApplication1\Web.config
文件 4437 2011-12-21 10:02 WebApplication1\WebApplication1\WebApplication1.csproj
文件 1373 2011-12-21 10:02 WebApplication1\WebApplication1\WebApplication1.csproj.user
............此处省略8个文件信息
- 上一篇:eprime 安装步骤
- 下一篇:回溯方法设计指派问题的算法
评论
共有 条评论