-

- 加为好友
- 发送私信
- 在线聊天
dawei362
- 等级:

- 可用分等级:
- 总技术专家分:
- 总技术专家分排名:
|
| 发表于:2008-04-28 19:53:132楼 得分:0 |
这时导出的代码 Response.Clear(); Response.Buffer = false; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=test.xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType = "application/ms-excel"; this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); GridView1.RenderControl(oHtmlTextWriter); //this.DataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); 这是导入的代码 string conpath = this.FileUpload1.PostedFile.FileName; string type = System.IO.Path.GetExtension(conpath); string filePath = Server.MapPath("upfiles/") + DateTime.Now.ToString("mmss") + System.IO.Path.GetFileName(conpath); // 假设 upload 为你上传的服务器目标的文件夹 FileUpload1.PostedFile.SaveAs(filePath); string constr = "provider=microsoft.jet.oledb.4.0;data source='" + filePath + "';Extended Properties=Excel 8.0;"; OleDbConnection excelConn = new OleDbConnection(constr); excelConn.Open(); DataTable dt = excelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string tname = dt.Rows[0]["TABLE_NAME"].ToString(); string sqqld = "SELECT * FROM [" + tname + "]"; OleDbCommand excelCmd = new OleDbCommand(sqqld, excelConn); (System.Configuration.ConfigurationManager.ConnectionStrings["nect46"].ConnectionString); SqlConnection accessConn = new SqlConnection("server=chenwei;database=stu;uid=sa;pwd=3116451"); accessConn.Open(); OleDbDataAdapter oleAdapter = new OleDbDataAdapter(excelCmd); DataSet myDs = new DataSet(); oleAdapter.Fill(myDs); GridView1.DataSource = myDs; if (myDs.Tables[0].Rows.Count > 0) { string strSql = ""; string CnnString = "server=chenwei;database=stu;uid=sa;pwd=3116451"; SqlConnection conn = new SqlConnection(CnnString); conn.Open(); SqlCommand myCmd = null; for (int i = 0; i < myDs.Tables[0].Rows.Count; i++) { strSql = "insert into XML(类型,题号,题目,选项1,选项2,选项3,选项4,分数,解答) values ('"; strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[4].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[5].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[6].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[7].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i].ItemArray[8].ToString() + "')"; try { myCmd = new SqlCommand(strSql, conn); myCmd.ExecuteNonQuery(); Label1.Text = " <script language=javascript>alert('数据导入成功.'); </script>"; } catch { Label1.Text = " <script language=javascript>alert('数据导入失败.'); </script>"; } } conn.Close(); } | | |
修改
删除
举报
引用
回复
| |