<p style="text-align: center"><span style="font-size: 18px">ASP.NET开发简单实用的方法
打印和导出EXCEL在目前ASP.NET开发中可以说是必要的,有时候针对不同数据难易程度下,用有效快速的方法是解决办法的有效途径之一。
/// /// 调用GOOGLE自带打印格式 /// /// /// protected void Btn_Printf_Click(object sender,EventArgs e) { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(),"","",false);//后台打印事件 }
/// /// 导出 /// /// /// protected void Btn_Output_Click(object sender,EventArgs e) { var FileName = DateTime.Now.ToString("yyyy-MM-dd"); System.Data.DataTable dt =需要打印数据; if (dt != null && dt.Rows.Count > 0) { CreateExcel_t(dt,FileName); } else { Response.Write(""); return; } }
/// /// 生成EXCEL /// /// /// public void CreateExcel_t(DataTable dt,string FileName) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentType = "application/vnd.ms-xls";
HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename=" + FileName + "导出EXEL名称.xls"); StringBuilder table = new StringBuilder(); // |
EXCEL标题
"); table.Append(dt.Columns[j].Caption.ToString());//表格的标题 table.Append(""); table.Append(dt.Rows[i][j].ToString()); table.Append("
//在aspx网页中
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
//在Page_Load中
AspNetPager1.RecordCount = (int)Cqzxw.SqlData.ExecuteScalar("Select count(*) From 表名");
ShowData();
//在数据绑定时
}
}
前端
后端
protected void AspNetPager1_PageChanged(object sender,EventArgs e) { //ShowData(); aspnetpager(); }
public void aspnetpager() { DataTable dt =?需要打印数据; string Msg = ToJson(dt); this.Lab_Count.Text = dt.Rows.Count.ToString(); //设置数量为DataTable的行数 AspNetPager2.RecordCount = dt.Rows.Count; //分页数据源对象 PagedDataSource pds = new PagedDataSource(); //设置为允许分页 pds.AllowPaging = true; //设置每一页的大小 (ASPNetPager1.PageSize在控件属性里面设置) pds.PageSize = this.AspNetPager2.PageSize; //当前页面索引是 aspnetpager控件页面索引-1,因为后者的CurrentPageIndex是1开始 pds.CurrentPageIndex = this.AspNetPager2.CurrentPageIndex - 1; //设置PageDataSource的数据源(DataView) pds.DataSource = dt.DefaultView; //设置Repeater的数据源(是PageDataSource) Repeater1.DataSource = pds; //绑定数据 Repeater1.DataBind(); }
WriteLog(string.Format(@"参数={0}、参数={1}、参数={2}、参数={3}、参数={4}、参数={5}",对应参数,对应参数));//编写日志方法
WriteLog("提示字符串"+需要传递的参数);
/// /// 在本地写入错误日志 /// private static readonly object writeFile = new object(); /// /// 在本地写入错误日志 /// /// public static void WriteLog(string debugstr) { lock (writeFile) { FileStream fs = null; StreamWriter sw = null;
try { //string filename = DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; string filename = DateTime.Now.ToString("yyyy-MM-dd") + ".Log.txt"; //服务器中日志目录 //string folder = HttpContext.Current.Server.MapPath("~/Log"); string folder = AppDomain.CurrentDomain.BaseDirectory + @"Log"; if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); fs = new FileStream(folder + "/" + filename,System.IO.FileMode.Append,System.IO.FileAccess.Write); sw = new StreamWriter(fs,Encoding.UTF8); sw.WriteLine(DateTime.Now.ToString() + " " + debugstr + "rn"); } finally { if (sw != null) { sw.Flush(); sw.Dispose(); sw = null; } if (fs != null) { // fs.Flush(); fs.Dispose(); fs = null; } } } }
/// /// DataTable解析Json数据【方法一】 /// /// /// public static string ToJson(DataTable dt) { StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); DataRowCollection drc = dt.Rows; for (int i = 0; i < drc.Count; i++) { jsonString.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { string strKey = dt.Columns[j].ColumnName; string strValue = drc[i][j].ToString(); Type type = dt.Columns[j].DataType; jsonString.Append(""" + strKey + "":"); strValue = StringFormat(strValue,type); if (j < dt.Columns.Count - 1) { jsonString.Append(strValue + ","); } else { jsonString.Append(strValue); } } jsonString.Append("},"); } jsonString.Remove(jsonString.Length - 1,1); jsonString.Append("]"); return jsonString.ToString(); }
/// /// dataTable转换成Json格式 【方法二】 /// /// /// public static string DataTable2Json(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("""); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("":""); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("","); } jsonBuilder.Remove(jsonBuilder.Length - 1,1); jsonBuilder.Append("},"); } if (dt.Rows.Count > 0) { jsonBuilder.Remove(jsonBuilder.Length - 1,1); } jsonBuilder.Append("]"); return jsonBuilder.ToString(); }
/// /// 格式化字符型、日期型、布尔型 /// private static string StringFormat(string str,Type type) { if (type == typeof(string)) { str = String2Json(str); str = """ + str + """; } else if (type == typeof(DateTime)) { str = """ + str + """; } else if (type == typeof(bool)) { str = str.ToLower(); } else if (type != typeof(string) && string.IsNullOrEmpty(str)) { str = """ + str + """; } return str; }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!