.NET 导出指定格式的Excel 或World
来源:互联网 发布:50岁知天命是什么成语 编辑:程序博客网 时间:2024/05/21 17:02
一、导出方法
- /// <summary>
- /// 导出指定格式的Excel或World信息
- /// </summary>
- /// <param name="FileName">导出文件名称</param>
- /// <param name="FilePath">导出文件路径</param>
- /// <param name="WorldContent">导出内容</param>
- /// <param name="type">导出类型,1为Excel、2为World</param>
- public static void ExportFile(String FileName, String FilePath, String WorldContent, Int32 type)
- {
- CreateFiles(HttpContext.Current.Server.MapPath(FilePath), WorldContent);
- HttpResponse res = HttpContext.Current.Response;
- res.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
- if (type != 1)
- res.ContentType = "application/vnd.ms-world";
- else
- res.ContentType = "application/vnd.ms-excel";
- res.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
- res.Write(WorldContent);
- res.Redirect(FilePath);
- res.End();
- }
- private static bool CreateFiles(string fileurl, string str)
- {
- try
- {
- File.WriteAllText(fileurl, str);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 导出详细
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btn_Export_Click(object sender, EventArgs e)
- {
- string fileName = "导出.doc";
- string content = ExportTabDetail();
- ExcelExport.ExportFile(fileName, "rptFiles/" + fileName, content,1);
- }
- /// <summary>
- /// 导出表详细
- /// </summary>
- /// <returns></returns>
- <p>private string ExportTabDetail()
- {
- StringBuilder sbAppent = new StringBuilder();
- int t_num = 1;
- string ids = string.Empty;
- //获取表的详细信息
- DataSet ds = bllTable.GetT_BaseList(this.txtUser.Value.Trim().Replace("'", "''"), drop_Type.SelectedValue, AnpGridView.CurrentPageIndex, AnpGridView.RecordCount);
- if (ds == null || ds.Tables[0].Rows.Count == 0) return null;
- sbAppent.Append("<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td align='center'>");
- foreach (DataRow rowTitle in ds.Tables[0].Rows)
- {
- if (int.Parse(rowTitle["TableType"].ToString()) > 2) continue;
- sbAppent.Append("<table border='1' bordercolor='#000000' bordercolordark='#FFFFFF' cellpadding='0' cellspacing='0' width='60%'>");
- sbAppent.Append("<tr><td colspan='6'>名称:" + rowTitle["TableName"] + "</td></tr>");
- sbAppent.Append("<tr>");
- sbAppent.Append("<td colspan='6' >描述:" + rowTitle["TableTitle"] + "</td>");
- sbAppent.Append("</tr>");
- sbAppent.Append("<tr style='background-color:#BFBFBF;font-family:@仿宋_GB2312;font-size:larger;text-align:left;'><td>数据元名称</td><td>数据源中文名称</td><td>数据类型</td><td>数据长度</td><td>默认值</td><td>是否关键字</td></tr>");
- string tabID = "'" + rowTitle["TableId"].ToString() + "'";
- DataSet datasetField = bll.GetFieldListByTabIds(tabID);
- if (datasetField == null || datasetField.Tables[0].Rows.Count == 0)
- continue;
- foreach (DataRow rowContent in datasetField.Tables[0].Rows)
- {
- string isPrimary = string.Empty;
- if (bool.Parse(rowContent["FieldIsPriKey"].ToString()) == true)
- isPrimary = "是";
- else
- isPrimary = "否";
- sbAppent.Append("<tr style='text-align:left;'><td style='width: 15%;'>" + rowContent["FieldName"] + "</td><td style='width: 20%;'>" + rowContent["FieldTitle"] + "</td><td style='width: 10%;'>" + rowContent["FieldType"] + "</td><td style='width: 10%;'>" + rowContent["FieldSize"] + "</td><td style='width: 8%;'>" + rowContent["DefaultValue"] + "</td><td style='width: 10%;'>" + isPrimary + "</td></tr>");
- }
- sbAppent.Append("</table>");
- t_num++;
- sbAppent.Append("<table border='0'><tr rowspan='3' cellpadding='0' cellspacing='0'><td> </td></tr></table>");</p><p> }
- sbAppent.Append("</td></tr></table>");
- return sbAppent.ToString();
- }</p>
0 0
- .NET 导出指定格式的Excel或World
- .NET 导出指定格式的Excel 或World
- 导出指定格式的excel
- java 根据excel模板格式导出指定格式的excel
- .net 导出复杂格式的EXCEL
- ASP.NET 导出模板格式的EXCEL
- 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法
- 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法
- 指定格式或者快速导出的EXCEL文件函数
- asp.net<Web版> ---GridView 数据导出到excel表,系列问题<一>---导出的Excel文件格式与文件扩展名指定的格式不一致
- asp.net gridview导出Excel、World文档
- poi导出excel指定单元格格式
- ASP.NET导出excel文件提示“未指定的错误”!
- asp.net页面指定控件导出为Word 或excel 文档
- ASP.NET导出Excel,打开Excel文件时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致”
- .net解决数据导出excel时的格式问题
- .net解决数据导出excel时的格式问题
- asp.net里导出excel表或Word的方法
- 在VMware中给Solaris增加新硬盘并进行分区和挂载
- OJ 1.O
- DIV层被Flash遮挡的解决办法
- 关于Android自定义控件的学习
- IOS静态库封装(二)
- .NET 导出指定格式的Excel 或World
- 先确定模型,再使用ENTER_FRAME
- Tmux:终端复用器
- boost 系列五 assign和format
- OJ 1.P
- 一言一行,一路平安,江辰护栏,与您共度一生
- htons函数详解
- 178: ISAM error: Database is locked; pending change to logging mode 解决办法整理
- mxGraph图形文本自动换行