2014年7月13日 星期日

[RESOLVED] Export ExcelFile to grid View


Hi, I want to import an excel file(file name: tp30.xls, having one sheet name: tp30) in a gridView. Every time I get a Exception....


Syntax error (missing operator) in query expression 'Valuated stock'.


near the line.....    da.Fill(ds);    ( or, da.Fill(ds,"[tp30$]"); )


here is my codes........


protected void Button2_Click(object sender, EventArgs e)
{
string connString = "";
string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
string path = FileUpload1.PostedFile.FileName;
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT Material,Valuated stock FROM [tp30$]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
da.Dispose();
conn.Close();
conn.Dispose();
}
}

The aspx code.....
































It simply does not understand that Valuated stock is one word and not 2 different names. 


Try


string query = "SELECT Material,[Valuated stock] FROM [tp30$]";


I have Solve it!!!



Hi San,

Try this

 string query = "SELECT Material,[Valuated stock] FROM [tp30$]";



Sql didn't allow space between the name of column.so you need to put the name in square bracket.

hope this will help you out .

沒有留言:

張貼留言