将Excel数据导出到Gridview不起作用 [英] Export the excel data to Gridview is not working

查看:67
本文介绍了将Excel数据导出到Gridview不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行模式下如下

i有一个浏览按钮和导入按钮。

当我点击浏览按钮时,用户必须选择文件并单击确定。 $>


在该用户之后选择导入按钮。

当用户点击导入按钮时,excel记录将显示在Gridview中。



我的代码如下

In run mode as follows
i have one Browse button and Import Button.
When i click the Browse Button user has to select the file and click ok.

After that user Select the Import Button.
When user clicks the Import button, the excel record will be displayed in the Gridview.

for that my code as follows

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

private void import()
{
string Filename;
LblFileName.Text = "";
Filename = fileupload.FileName;
if (Filename == "")
{
//return "File is not selected";
}

LblFileName.Text = "File Name : " + Filename.ToString();
string path = Filename.ToString();
GvSch.DataSource = ImportExcelXLS(path, false);
GvSch.DataBind();
}


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();



if(row[3].ToString().Contains("+"))

row1["Date"] = datevalue;
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();

if (row[5].ToString().Contains("+"))
row1["Date"] = datevalue;
row1["Course"] = row[1].ToString();
row1["Session"] = "2";
row1["Subject"] = row[2].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);
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;
}





在运行模式下如下



请点击浏览按钮并选择excel文件并单击确定。

然后单击导入按钮。

当我单击导入按钮时出现如下错误



IListSource 不包含任何数据源。



错误行显示在下面的行如下



In run mode as follows

Click the Browse Button and select the excel file and click ok.
And after that click Import button.
When i click the import buttonshows error as follows

The IListSource does not contain any data sources.

The error line shows in below line as follows

GvSch.DataBind();



请帮助我。从上面的代码中我犯了什么错误。



问候,

Narasiman P.







更正格式问题。

已添加标签。

[/ Edit]

推荐答案





提供文件的完整路径用于文件名参数的ImportExcelXLS函数中的名称。



例如c:\\abc.xlsx现在正在使用abc.xlsx



这将解决您的问题。
Hi,

Provide the full path with file name in ImportExcelXLS function for filename parameter.

for example c:\\abc.xlsx right now it is taking abc.xlsx

That will resolve your problem.


这篇关于将Excel数据导出到Gridview不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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