前台的话不多说,本次只详细说下后台导出数据库中的二进制字段,上传下次有时间再说,有比较急的可以留言我
之前在网上搜了关于下载导出二进制字段的资料,发现都不是我想要的,都只是把上传的文件放到服务器然后下载这个文件,并不是把数据库中的二进制使用excel导出,下面我就贴出下我自己写的方法,可以供给需要的人,当然也有大牛可能比我写的好,您可以选择不看或提点下建议!
最后本文数原创,转载请注明出处或链接!
/// <summary> /// 下载卡券导入日志 /// </summary> /// <param name="e"></param> private void EditBLL(DataGridCommandEventArgs e) { if (e.CommandName == "DownLoad")//e 前台绑定事件commandName { String sFileId = e.Item.Cells[0].Text.Trim(); VirtualCouponsManagebll bll = new VirtualCouponsManagebll(); DataTable dt = bll.GetCardcouponsFileLogToExcel(sFileId);//查询出数据 if (dt.Rows.Count <= 0) { MessageBox("查询卡券导入日志失败"); return; } string sGoodsName = dt.Rows[0]["GoodsName"].ToString(); string sCreateTime = Convert.ToDateTime(dt.Rows[0]["CreateDate"].ToString()).ToString("yyyyMMddHHmmss"); string sFileName = sGoodsName + sCreateTime + ".xlsx"; DataSet ds = StreamConvertDataSet((byte[])dt.Rows[0]["CardCouponsFile"], sFileName); if (ds == null || ds.Tables[0].Rows.Count <= 0) { MessageBox("卡券数据由byte数组转DataSet失败"); return; } ExportTableStrBul(ds.Tables[0], "虚拟商品:" + sGoodsName + "_卡券导入数据", sCreateTime); } }
/// <summary> /// byte数组转成DataSet /// </summary> /// <param name="bytes"></param> /// <param name="sFileName"></param> /// <returns></returns> private DataSet StreamConvertDataSet(byte[] bytes, string sFileName) { MemoryStream _MStream = new MemoryStream(bytes); string path = AppDomain.CurrentDomain.DynamicDirectory + sFileName; System.IO.FileStream Filestream = new System.IO.FileStream(path, System.IO.FileMode.Create); _MStream.CopyTo(Filestream); IExcelDataReader excelReader = null; DataSet ds = null;
try { excelReader = ExcelReaderFactory.CreateOpenXmlReader(Filestream);
excelReader.IsFirstRowAsColumnNames = true; ds = excelReader.AsDataSet(); } catch (Exception) { Filestream.Close(); _MStream.Close(); excelReader.Close(); } finally { excelReader.Close(); Filestream.Close(); _MStream.Close(); if (File.Exists(path)) { File.Delete(path); } } return ds; }
/// <summary> /// 生成EXCEL /// </summary> /// <param name="dt">数据</param> /// <param name="sFileName">文件名</param> /// <param name="sCreateTime">文件名</param> public void ExportTableStrBul(DataTable dt, string sFileName, string sCreateTime) { Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sFileName) + sCreateTime + ".xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文 Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 EnableViewState = false;
DataView view = dt.DefaultView; string[] strArr = { "卡券", "结束日期" };//"TotalRMB", //从 DataView 创建新的 DataTable,可以使用 ToTable 方法将所有行和列或数据的一个子集复制到新的 DataTable 中 DataTable newTable = view.ToTable("DownTab", true, strArr);
dt = newTable;
StringBuilder sb = new StringBuilder(); int count = 0;
//--写入EXCEL sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"); sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">"); //写出列名 sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">"); foreach (DataColumn column in dt.Columns) { sb.AppendLine("<td>" + column.ColumnName + "</td>"); } sb.AppendLine("</tr>"); sb = sb.Replace("卡券", "卡券"); sb = sb.Replace("结束日期", "结束日期");
//写出数据 foreach (DataRow row in dt.Rows) { sb.Append("<tr>"); foreach (DataColumn column in dt.Columns) { if (column.ColumnName.Equals("卡券")) { sb.Append("<td style=\"vnd.ms-excel.numberformat:@\">" + ReplaceChar(row[column].ToString()) + "</td>"); } else { sb.Append("<td>" + row[column].ToString() + "</td>"); } } sb.AppendLine("</tr>"); count++; } sb.AppendLine("</table>");
Response.Write(sb.ToString()); Response.End(); }
|
请发表评论