首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

datatable Image导出EXCEL,该怎么解决

2012-04-18 
datatable Image导出EXCELpublic void Bind(System.Data.DataTable dt){if (dt null){return}else{#re

datatable Image导出EXCEL
public void Bind(System.Data.DataTable dt)
  {
  if (dt == null)
  {
  return;
  }
  else
  {
  #region 操作excel
  Excel.Workbook xlWorkBook;
  Excel.Worksheet xlWorkSheet;
  xlWorkBook = new Excel.Application().Workbooks.Add(Type.Missing);
  xlWorkBook.Application.Visible = false;
  xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
  #endregion
  //设置标题
  int rowIndex = 1; //行
  int columnIndex = 0; //列
  //将DataTable的列名导入Excel表第一行
  foreach (DataColumn dc in dt.Columns)
  {
  columnIndex++;
  xlWorkSheet.Cells[rowIndex, columnIndex] = dc.ColumnName;
  //定义标头的宽度
  ((Excel.Range)xlWorkSheet.Cells[columnIndex, 11]).ColumnWidth = 10;
  ((Excel.Range)xlWorkSheet.Cells[columnIndex, 12]).ColumnWidth = 10;
  ((Excel.Range)xlWorkSheet.Cells[columnIndex, 15]).ColumnWidth = 10;
  ((Excel.Range)xlWorkSheet.Cells[columnIndex, 23]).ColumnWidth = 15;
  }
  //将DataTable中的数据导入Excel中
  for (int i = 0; i < dt.Rows.Count; i++)
  {
  rowIndex++;
  xlWorkSheet.Cells[rowIndex, 1] = dt.Rows[i]["uid"].ToString();
  xlWorkSheet.Cells[rowIndex, 2] = dt.Rows[i]["FRID"].ToString();
  xlWorkSheet.Cells[rowIndex, 3] = dt.Rows[i]["name"].ToString();
  xlWorkSheet.Cells[rowIndex, 4] = dt.Rows[i]["Age"].ToString();
  xlWorkSheet.Cells[rowIndex, 5] = dt.Rows[i]["Address"].ToString();
  xlWorkSheet.Cells[rowIndex, 6] = dt.Rows[i]["Aliases"].ToString();
  xlWorkSheet.Cells[rowIndex, 7] = dt.Rows[i]["CrimeName"].ToString();
  xlWorkSheet.Cells[rowIndex, 8] = dt.Rows[i]["Diploma"].ToString();
  xlWorkSheet.Cells[rowIndex, 9] = dt.Rows[i]["Nation"].ToString();
  xlWorkSheet.Cells[rowIndex, 10] = dt.Rows[i]["ImprisonTerm"].ToString();
  xlWorkSheet.Cells[rowIndex, 11] = Convert.ToDateTime(dt.Rows[i]["StartImprison"].ToString()).ToString("yyyy-MM-dd");
  xlWorkSheet.Cells[rowIndex, 12] = Convert.ToDateTime(dt.Rows[i]["StopImprison"].ToString()).ToString("yyyy-MM-dd");
  xlWorkSheet.Cells[rowIndex, 13] = dt.Rows[i]["PreMetier"].ToString();
  xlWorkSheet.Cells[rowIndex, 14] = dt.Rows[i]["Wedlock"].ToString();
  xlWorkSheet.Cells[rowIndex, 15] = Convert.ToDateTime(dt.Rows[i]["EnterPrisonTime"].ToString()).ToString("yyyy-MM-dd");
  xlWorkSheet.Cells[rowIndex, 16] = dt.Rows[i]["DelictTime"].ToString();
  xlWorkSheet.Cells[rowIndex, 17] = dt.Rows[i]["NativePlace"].ToString();
  xlWorkSheet.Cells[rowIndex, 18] = dt.Rows[i]["Religion"].ToString();
  xlWorkSheet.Cells[rowIndex, 19] = dt.Rows[i]["RewardAndPunish"].ToString();
  xlWorkSheet.Cells[rowIndex, 20] = dt.Rows[i]["Commute"].ToString();
  xlWorkSheet.Cells[rowIndex, 21] = dt.Rows[i]["BodyStatus"].ToString();
  xlWorkSheet.Cells[rowIndex, 22] = dt.Rows[i]["LinkMan"].ToString();


  xlWorkSheet.Cells[rowIndex, 23] = dt.Rows[i]["ContactTel"].ToString();
  //
  string filename = Server.MapPath("1.gif");
  if (dt.Rows[i]["Photo"] == DBNull.Value)
  {
  xlWorkSheet.Cells[rowIndex, 24] = "暂无图片!";
  }
  else
  {
  byte[] filedata = (byte[])dt.Rows[i]["Photo"];
  System.IO.MemoryStream ms = new System.IO.MemoryStream(filedata);
  System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
  img.Save(filename);


  //int rangeindex = 16 * i + 8;
  //string rangename = "D" + rangeindex;
  //Excel.Range range = xlWorkSheet.get_Range("A8", Type.Missing);
  //range.Select();


  Excel.Pictures pict = (Excel.Pictures)xlWorkSheet.Pictures(Type.Missing);
  pict.Insert(filename, Type.Missing);
   
  // xlWorkSheet.Cells[rowIndex, 24];
  }
  xlWorkSheet.Cells[rowIndex, 25] = dt.Rows[i]["AdminID"].ToString();
  xlWorkSheet.Cells[rowIndex, 26] = dt.Rows[i]["AssessmentID"].ToString();
  xlWorkSheet.Cells[rowIndex, 27] = dt.Rows[i]["PersonnelID"].ToString();
  }
  #region 保存excel文件
  string filePath = Server.MapPath("ReadExcel") + "" + System.DateTime.Now.ToString().Replace(":", "") + "导出.xls";
  xlWorkBook.SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  xlWorkBook.Application.Quit();
  xlWorkSheet = null;
  xlWorkBook = null;
  GC.Collect();
  System.GC.WaitForPendingFinalizers();
  #endregion
  #region 导出到客户端
  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出", System.Text.Encoding.UTF8) + ".xls");
  Response.ContentType = "Application/excel";
  Response.WriteFile(filePath);
  Response.End();
  #endregion
  //结束进程
  EndExcel("EXCEL");
  }

  }

这样导出是可以。但是图片我想吧固定大小,放到固定的位置。求方法。求代码。
搞了1天了,都没弄出来。郁闷。

[解决办法]
你可以查看
http://blog.csdn.net/net_lover/article/details/1702797

里面有参数的说明
[解决办法]
http://blog.csdn.net/bkq421511585/article/details/7319783

里面有参数和代码的说明

热点排行