一、导出方法
/// <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>
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>");
}
sbAppent.Append("</td></tr></table>");
return sbAppent.ToString();
}