使用csharp在asp.net中使用import选项导出excel文件 [英] EXport the excel file using import option in asp.net using csharp

查看:69
本文介绍了使用csharp在asp.net中使用import选项导出excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将excel文件数据导出到gridview。



该代码如下

I am exporting the excel file data into gridview.

for that code as follows

protected void btnimport_Click(object sender, EventArgs e)
{
   import();
}


private void import()
{
   string path = @"C:\FACULTY TT N.xlsx";
   GvSch.DataSource = ImportExcelXLS(path, false);
   GvSch.DataBind();
   GvSch.Visible = true;
}


public static DataSet ImportExcelXLS(string FileName, bool hasHeaders)
{
   string HDR = hasHeaders ? "Yes" : "No";
   string strConn;
   if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
      strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
   else
      strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";

   DataSet output = new DataSet();

   using (OleDbConnection conn = new OleDbConnection(strConn))
   {
      conn.Open();

      DataTable schemaTable = conn.GetOleDbSchemaTable(
                OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

      foreach (DataRow schemaRow in schemaTable.Rows)
      {
         string sheet = schemaRow["TABLE_NAME"].ToString();

         if (!sheet.EndsWith("_"))
         {
            try
            {
               OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
               cmd.CommandType = CommandType.Text;

               DataTable outputTable = new DataTable(sheet);
               output.Tables.Add(outputTable);
               new OleDbDataAdapter(cmd).Fill(outputTable);

               DataTable table = new DataTable();
               table.Columns.Add("Date", typeof(string));
               table.Columns.Add("Course", typeof(string));
               table.Columns.Add("Session", typeof(string));
               table.Columns.Add("Subject", typeof(string));
               table.Columns.Add("Faculty", typeof(string));

               int irow = 0;
               foreach (DataRow row in outputTable.Rows)
               {
                  if (row[1].ToString() != "Course" && row[1].ToString() != "" && row[1].ToString() != null)
                  {
                     DataRow row1 = table.NewRow();
                     row1["Date"] = row[0].ToString();

                     row1["Course"] = row[1].ToString();
                     row1["Session"] = "1";
                     row1["Subject"] = row[2].ToString();
                     row1["Faculty"] = row[3].ToString();
                                
                     if (row[2].ToString().Trim() != "" && row[3].ToString().Trim() != "" && row[2].ToString().Trim() != null && row[3].ToString().Trim() != null)
                        table.Rows.Add(row1);
                     row1 = table.NewRow();
                     row1["Date"] = row[0].ToString();
                     row1["Course"] = row[1].ToString();
                     row1["Session"] = "2";
                     row1["Subject"] = row[4].ToString();
                     row1["Faculty"] = row[5].ToString();
                               
                     if (row[4].ToString().Trim() != "" && row[5].ToString().Trim() != "" && row[4].ToString().Trim() != null && row[5].ToString().Trim() != null)
                        table.Rows.Add(row1);
                  }
                  irow++;
               }
               output.Tables.Clear();
               output.Tables.Add(table);
            }
            catch (Exception ex)
            {
               throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
            }
            finally
            {
            }
         }
      }
   }
   return output;
}



在运行模式下,当我点击导入按钮时,在Gridview中,excel记录显示在gridview中。



但我希望用户选择excel文件,当用户点击浏览按钮并单击确定时。



然后用户点击导入按钮然后只有excel记录显示在gridview中。



在编码中我特意提到路径手动

字符串path = @C:\ FACULTY TT N.xlsx;



i希望用户选择excel文件并点击ok.then单击gridview记录中的导入按钮进行显示。



我怎样才能在asp.net中使用c#



问候,

Narasiman P.


In run mode when i click the Import Button, in Gridview the excel records are displayed in the gridview.

But i want the user to select the excel file, when user click the browse button and click ok .

Then user click the Import button then only excel record to be displayed in the gridview.

In coding i specifically mention the Path manually
string path = @"C:\FACULTY TT N.xlsx";

i want user to select the excel file and click ok.then click the import button in gridview record to be displayed.

for that how can i do in asp.net using c#

Regards,
Narasiman P.

推荐答案

请参阅链接:



http://dotnetawesome.blogspot.in /十一分之二千零一十三/如何-TO- import-export-database-data-from_18.html [ ^ ]


这篇关于使用csharp在asp.net中使用import选项导出excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆