æ¹å¼å¾å¤ï¼æä¾ä¸ç§æ¹å¼ï¼ç´æ¥ä½¿ç¨ADO.netçOLEDBå°±å¯ä»¥è¿æ¥Excelï¼åæä½å
³ç³»åæ°æ®åºæ¯ä¸æ ·çã
å¦ï¼
å°èæç®å½ä¸çTempä½ä¸ºä¸´æ¶æ件ç®å½ãstring urlPath = HttpContext.Current.Request.ApplicationPath + "/Temp/";string physicPath = HttpContext.Current.Server.MapPath(urlPath);string fileName = Guid.NewGuid() + ".Xls"; string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + physicPath + fileName +";Extended Properties=Excel 8.0;";OleDbConnection objConn = new OleDbConnection(connString);
OleDbCommand objCmd = new OleDbCommand();
objCmd.Connection = objConn;建ç«è¡¨ç»æobjCmd.CommandText = @"CREATE TABLE 客æ·ä¿¡æ¯
(
客æ·å varchar,
注åæ¶é´ varchar
)
";objCmd.ExecuteNonQuery();æå
¥æ°æ°æ®//建ç«æå
¥å¨ä½çCommand
objCmd.CommandText = "INSERT INTO 客æ·èµæ(客æ·å, çæ¥) VALUES (@CustomerName, @RegisterTime)";
objCmd.Parameters.Add(new OleDbParameter("@CustomerName", OleDbType.VarChar));
objCmd.Parameters.Add(new OleDbParameter("@RegisterTime", OleDbType.VarChar));
//éåDataSetå°æ°æ®æå
¥æ°å»ºçExcelæ件ä¸ï¼customerInfo为æ们ä»æ°æ®åºä¸è¯»å°çæ°æ®
foreach (DataRow row in customerInfo.Tables[0].Rows)
{
for (int i=0; i<parm.length; i++)="">
{
parm[i].Value = row[i];
}
objCmd.ExecuteNonQuery();
}</parm.length;>æä¾ä¸è½½HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.WriteFile(path + fileName);
string httpHeader="attachment;filename=backup.Xls";
response.AppendHeader("Content-Disposition", httpHeader);
response.Flush();System.IO.File.Delete(path + fileName);//å é¤ä¸´æ¶æ件
response.End();
温馨提示:答案为网友推荐,仅供参考