asp.netアプリケーションのExcelシートへのエクスポートとインポートにFollowin LOCを使用しています
protected void LinkbuttonExportToExcel_Click(object sender, EventArgs e)
{
UserManager manager = new UserManager();
DataSet dataSet = manager.GetProductDataToExport();
string attachment = "attachment; filename=Report.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
string tab1 = "";
foreach (DataTable table in dataSet.Tables)
{
foreach (DataColumn column in table.Columns)
{
Response.Write(tab1 + column.ColumnName);
tab1 = "\t";
}
}
tab = "\n";
foreach (DataRow dr in dataSet.Tables[0].Rows)
{
for (int i = 0; i < dataSet.Tables[0].Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
tab = "\n";
}
Response.End();
}
protected void ButtonImportDataFromExcel_Click(object sender, EventArgs e)
{
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/App_Data/ProductImport.xls"));
OleDbConnection objXConn = new OleDbConnection();
objXConn.ConnectionString = ConfigurationManager.ConnectionStrings["sqlXCon"].ConnectionString;
if (objXConn.State == ConnectionState.Closed)
{
objXConn.Open();
}
DataTable dt = new DataTable();
dt = objXConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string excelSheetName = string.Empty; ;
foreach (DataRow row in dt.Rows)
{
excelSheetName = row["TABLE_NAME"].ToString();
}
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheetName + "]", objXConn);
OleDbDataReader rd = cmd.ExecuteReader();
UserManager manager = new UserManager();
while (rd.Read())
{
int productId = int.Parse((rd.GetValue(0).ToString()));
string productName = (rd.GetValue(1).ToString());
string productNameHindi = (rd.GetValue(2).ToString());
decimal productPrice = decimal.Parse((rd.GetValue(3).ToString()));
string productStatus = (rd.GetValue(4).ToString());
manager.UpdateProductMasterFromExcelSheet(productId, productName);
}
}
else
{
objXConn.Close();
}
msgBox1.alert("Products updated successfully");
}
}
私は次の接続文字列を使用しています..
<add name="sqlXCon" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='|DataDirectory|ProductImport.xls';Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';" />
ファイル Report.xls が正常にエクスポートされます。しかし、問題は、ファイルを開いて再度 .xls として保存しない限り、「Report.xls」をインポートできないことです。なぜこれが起こっているのですか?