我从GridView中导出为Excel的代码如下:   /// <summary>
    /// 从GridView中将数据导入到Excel中
    /// </summary>
    /// <param name="ctl">GridView</param>
    public static void ToExcel(System.Web.UI.Control ctl)
    {
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        string strStyle = "<style>td{mso-number-format:\"\\@\";}</style>";
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        tw.WriteLine(strStyle);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }
但是以文本方式打开导出的Excel文件, 发现里面是HTML代码, 我现在想要在导出的Excel文件修改后再导入到数据库中, 该如何该作.

解决方案 »

  1.   

    那你到出到excel就不要用导出html的形式。。
      

  2.   

    我现在想要在导出的Excel文件修改后再导入到数据库中
    那导出excel有意义么,反正最终要保存到数据库里的,不如以grid形式展现,可编辑,可随时导出为excel
    如果你只想做到类似excel那种操作效果,要么用ajax模拟一个,要么找插件,我记得有activex的,还有以数据库形式直接呈现的,类似sharepoint里的datasheet
      

  3.   

    1.為什麽要導出excel再導入到數據庫。你能有gridview就一定有一個dataset,直接將dataset直接導入到數據庫中不是更好?
    2.導出就不要導成html格式。導成真正的excel。DataTable導出到EXCEL的代碼如下,sheet名字和excel名自己對應。    /// <summary>
        /// DataTable導出到Excel.繁體OS,無亂碼問題.
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="strFileName">含.xls後綴</param>
        public void DownloadAsExcel(DataTable dt, string strFileName)
        {
            try
            {
                StringWriter sw = new StringWriter();
                string colstr = "";
                foreach (DataColumn col in dt.Columns)
                {
                    colstr += col.ColumnName + "\t";
                }
                sw.WriteLine(colstr);            foreach (DataRow row in dt.Rows)
                {
                    colstr = "";
                    foreach (DataColumn col in dt.Columns)
                    {
                        colstr += row[col.ColumnName].ToString() + "\t";
                    }
                    sw.WriteLine(colstr);
                }
                sw.Close();
                System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + "");
                System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
                System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
                System.Web.HttpContext.Current.Response.Write(sw);
                System.Web.HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
    從excel導入到數據庫如下,我用了fileupload控件。if (FileUpload1.PostedFile.ContentLength > 0)
            {
                try
                {
                    string ServiceUrl = "";
                    string strSaveDir = "E:\\System\\Scrap Report\\UploadFiles";
                    string strName = FileUpload1.PostedFile.FileName;
                    int inExt = strName.LastIndexOf(".");
                    string strExt = strName.Substring(inExt);
                    if (strExt.ToLower() != ".xls")
                    {
                        lblMessage.Text = "只支持Excel文檔.";
                        return;
                    }
                    DateTime dateNow = DateTime.Now;
                    string strNewName1 = dateNow.Year.ToString() + dateNow.Month.ToString() + dateNow.Day.ToString() + dateNow.Millisecond.ToString() + FileUpload1.PostedFile.ContentLength.ToString();//dateNow.DayOfYear.ToString() + dateNow.Millisecond + FileUpload1.PostedFile.ContentLength.ToString();
                    int intPath = strName.LastIndexOf("\\");
                    string strNewName = "\\" + strNewName1 + strName.Substring(intPath + 1);                FileUpload1.PostedFile.SaveAs(strSaveDir + strNewName);
                    ServiceUrl = strSaveDir + strNewName;                string dataSource = FileUpload1.PostedFile.FileName;
                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ServiceUrl + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";                OleDbConnection conn = new OleDbConnection(strConn);
                    OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
                    DataSet myDataSet = new DataSet();
                    myCommand.Fill(myDataSet);
                    dt = myDataSet.Tables[0];
                    lblMessage.Text = Save(dt);
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
            }
            else
            {
                lblMessage.Text = "請上傳Excel文件.";
            }
      

  4.   

    你还真复杂。还搞到 EXCEL后 再保存为什么不GRID 就编辑好了 直接保存???需要EXCEL?,那保存好了,再导出到 EXCEL