第一,首先要实现将这几个文件读进来
然后提取文件内容
就是通过aspxgridview显示指定文件夹中几个excel文件的内容,格式都是一样的
比如时间,名称,等
或者说是显示excel文件中某个单元格中的数据
然后提取文件内容
就是通过aspxgridview显示指定文件夹中几个excel文件的内容,格式都是一样的
比如时间,名称,等
或者说是显示excel文件中某个单元格中的数据
解决方案 »
- GridView重新绑定数据后不显示
- 怎么根据选中的节点知道它是第几个节点
- asp.net 分层问题?
- 我在datagrid中加入了radiobutton,请问应该怎样获取datagird中的一条记录?
- [着急]一个页面有两个二级联动菜单,单独用其中一个都可以用,一起却不能用??
- 帮助介绍两本asp.net控件设计的好书,明天去买。。。
- 关于ASP.NET的数据库查询问题,菜鸟求助(50分问题)
- asp.net如何给需要smtp认证的地址发邮件(我的外号是给就100分)
- vs2012 有断点进行调试就会卡死,重启
- 下面的SQL语句怎么改,谢谢
- 问个request time out的问题
- 一个后台登陆的问题
wuyq11
来回答
方法2: 用Excel COM组件,操作Excel获取内容,赋值到DataTable一类的数据集上,
再绑定到显示控件上。http://blog.csdn.net/fangxinggood/archive/2006/04/08/655313.aspx
string[] files = Directory.GetDirectories("D:\\", "*.*", SearchOption.AllDirectories);
foreach(string f in files)
{
FileInfo fi = new FileInfo(f);
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
using(OleDbConnection OleConn = new OleDbConnection(strConn))
{
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn); OleDaExcel.Fill(ds);
OleConn.Close();
}}
try
{
if ((FileUpload1.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
ds = dst;
string query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string strFileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
FileUpload1.SaveAs(Server.MapPath("~/duibiupfiles/" + strFileName + strFileType));
}
else
{
Label1.Text = "只可是EXCEL文件";
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Visible = true;
return;
}
string strNewPath = Server.MapPath("~/duibiupfiles/" + strFileName + strFileType); //Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
}
query = "SELECT * FROM [sheet1$]";
conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
{
conn.Open(); cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds); GridView1.DataSource = ds.Tables[0];
GridView1.DataBind(); Label1.Text = "导入成功,总行数为" + ds.Tables[0].Rows.Count;
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Visible = true; da.Dispose();
conn.Close();
conn.Dispose();
//将excel导入到sql的c表里
string strsql = "select * into x from OpenRowset ('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=" + strNewPath + "','select * from [sheet1$]')";
SqlConnection con = new SqlConnection("server=localhost;database=FFI_Product;uid=sa;pwd=sa");
SqlCommand sqlcomm = new SqlCommand(strsql, con);
SqlDataAdapter sdaa = new SqlDataAdapter();
sdaa.SelectCommand = sqlcomm;
con.Open();
DataSet dss = new DataSet();
sdaa.Fill(dss);
con.Close();
}
else
{
Label1.Text = "请选择EXCEL文件!";
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Visible = true;
}
}
}
catch
{
Label1.Text = "请先清除缓存";
Label1.ForeColor = System.Drawing.Color.Blue;
}