Excel로 내보내기 및 Excel 가져 오기가있는 gridview가 있습니다. 그러나 성공적으로 gridview에서 데이터를 내보낼 수 있고 같은 엑셀로 가져 오려고하면 그 Excel을 가져올 수 없습니다. 하지만 파일 이름을 변경하고 가져올 때 제대로 작동합니다.Excel을 표로 가져올 수 없습니다.
아무도 나에게 이런 일이 일어나는 이유를 제안 할 수 있습니까? 아래는 Import to Grid to excel에 대한 코드입니다.
protected void btnImport_Click(object sender, EventArgs e)
{
string sexcelconnectionstring = "";
string strFileType = Path.GetExtension(FileUploadExcel.FileName).ToLower();
string path = FileUploadExcel.PostedFile.FileName;
string query = "";
Label1.Text = "";
GridView2.Visible = false;
if (strFileType != String.Empty)
{
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
}
else if (strFileType.Trim() == ".xlsx")
{
sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection conn = new OleDbConnection(sexcelconnectionstring);
if (conn.State == ConnectionState.Closed)
conn.Open();
System.Data.DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname = dt.Rows[0]["Table_Name"].ToString();
try
{
query = "SELECT [Size],[Order],[Ratio] FROM [" + sheetname + "]";
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (objDA.IsValidExcelColumns(ds, GridView2, strSqlTable))
{
string sclearsql = "delete from " + strSqlTable;
SqlConnection sqlconn = new SqlConnection(strConnectionString);
SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();
//series of commands to bulk copy data from the excel file into our sql table
OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
oledbconn.Open();
OleDbCommand oledbcmd = new OleDbCommand(query, oledbconn);
OleDbDataReader dReader;
dReader = oledbcmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnectionString);
sqlBulk.DestinationTableName = strSqlTable;
sqlBulk.WriteToServer(dReader);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Data has been saved successfully.')", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please validate the excel data.')", true);
}
}
catch (Exception ex)
{
Label1.Text = "Upload status: The file could not be uploaded due to invalid column names.Please check: " + ex.Message;
}
conn.Close();
conn.Dispose();
objDA.BindGrid(GridView1, "select * from " + strSqlTable);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a file to import the data.')", true);
}
}
다음은 Excel로 내보내는 코드입니다.
하는 행에서 오류가 발생하고 오류가 무엇입니까? –
이 줄에서 발생합니다 conn.Open(); sexcelconnectionstring 바로 아래에. 오류 : 사용자 코드가 OleDbException을 처리하지 못했습니다. 외부 테이블이 예상 된 형식이 아닙니다. –
제안 사항 : 오래된 xls 파일을로드 할 필요가없는 경우 안전하게 epplus와 같은 라이브러리를 사용하여 파일을로드 할 수 있습니다. 그것은 더 빠르고 훨씬 더 신뢰성있는 것입니다. –