导入导出xls数据
/// <summary> /// 导出为Word /// </summary> /// <param name="filename"></param> /// <param name="objControl"></param> protected void OutExportWord(string filename, Control objControl) { #region HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.ContentType = "application/msword"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename) + ".doc"); objControl.EnableViewState = false; CultureInfo cult = new CultureInfo("zh-CN", true); StringWriter sw = new StringWriter(cult); HtmlTextWriter writer = new HtmlTextWriter(sw); objControl.RenderControl(writer); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); #endregion }
/// <summary> /// 导入xls数据 /// </summary> /// <param name="filename">导入的Xls文件名</param> /// <returns></returns> public DataTable ExportXls(string filename) { #region OleDbConnection dbf_conn = null; DataTable dt = null; string sqlMaster = ""; try { string oleDBConnString = String.Empty; oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;"; oleDBConnString += "Data Source="; oleDBConnString += Server.HtmlDecode(filename); oleDBConnString += ";Extended Properties='Excel 8.0;IMEX=1;'"; dbf_conn = new OleDbConnection(oleDBConnString); dbf_conn.Open(); dt = new DataTable(); // dt = null; dt = dbf_conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); } catch { Alert("无法连接到数据源!"); return null; } try { sqlMaster = " SELECT * FROM [Sheet1$]";//指定某列开始读取:Sheet1$B1:B5 OleDbDataAdapter odbcAdMaster = new OleDbDataAdapter(sqlMaster, dbf_conn); odbcAdMaster.Fill(dt); return dt; } catch { Alert("Excel工作表名称必须为Sheet1,或者删除Excel多余的列!"); return null; } #endregion }
/// <summary> /// 数据导出 /// </summary> /// <param name="dt"></param> /// <param name="filename"></param> /// <Muser>AllenCheng</Muser> protected void OutExportXls(DataTable dt, string filename) { #region if (dt != null) { HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.ContentType = "application/ms-excel/msword"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename) + ".xls"); DataGrid dg = new DataGrid(); dg.DataSource = dt; dg.AllowPaging = false; dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(dg_ItemDataBound1); dg.DataBind(); CultureInfo cult = new CultureInfo("zh-CN", true); StringWriter sw = new StringWriter(cult); HtmlTextWriter writer = new HtmlTextWriter(sw); writer.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\">"); writer.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">"); dg.RenderControl(writer); dg.Dispose(); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } #endregion }