日期:2014-05-17  浏览次数:20499 次

把服务器上的数据导入到excel表中并且下载到本地
public class ExportDataToExcel : IHttpHandler {
   
  public void ProcessRequest (HttpContext context) {
  context.Response.ContentType = "application/vnd.ms-excel";
  string fileName = "~/ExportFile";
  fileName = context.Server.MapPath(fileName);
  fileName = fileName + "\\" + new Random().Next().ToString() + ".xls";
  if (File.Exists(fileName))
  {
  File.Delete(fileName); 
  }
  Office.Application excel = new Office.Application();
  excel.SheetsInNewWorkbook = 1;
  excel.Workbooks.Add();
  Office.Worksheet sheet = (Office.Worksheet)excel.ActiveWorkbook.Sheets[1];
  sheet.Name = "BookInfo";
  Office.XlFileFormat fileType = Office.XlFileFormat.xlWorkbookNormal;
  int CategoryId = int.Parse(context.Request.QueryString["CategoryId"]);
  DataTable table = BookManage.GetBookInfoTableByCategoryId(CategoryId);
  for (int index = 0; index < table.Columns.Count; index++)
  {
  sheet.Cells[1, index + 1] = table.Columns[index].ColumnName;
  }
  for (int i = 0; i < table.Rows.Count; i++)
  {
  for (int j = 0; j < table.Columns.Count; j++)
  {
  sheet.Cells[i + 2, j + 1] = table.Rows[i][j].ToString();
  }
  }
  excel.Visible = false;
  excel.ActiveWorkbook.SaveAs(fileName,fileType);
  excel.ActiveWorkbook.Close();
  context.Response.WriteFile(fileName);  
  context.Response.Flush();
  context.Response.End();


这是做的一个一般处理程序,把数据导入到excel表中成功了,但是在从服务器下载到本地时下载的文件确是这个一般处理程序文件本身,不是excel表,哪位大神帮我看看!!!!

------解决方案--------------------
你 弄的好麻烦呀。。。 你用什么控件绑定数据
------解决方案--------------------
<asp:Repeater ID="Repeater1" runat="server"> 
<HeaderTemplate><table border="1" cellspacing="1" cellpadding="2" class="stock" id="Negative" bgcolor="#FFFFFF" align="center" width="99%">
<tr> 
<td height="22" align="center" class="stocktd" colspan="6" style="color:#93C; font-weight:bold; font-size:14px;">从<span style="color:#F0F"><%= date1.Text%></span>到<span style="color:#F0F"><%= date2.Text %></span>没有任何出入库操作的产品</td>
</tr>
<tr class="classtop1" id="title">
<td width="10%" height="20" align="center" class="stocktd0" style="font-weight:bold;">序号</td>
<td width="20%" align="center" class="stocktd0" style="font-weight:bold;" >产品编号</td>
<td width="18%" align="center" class="stocktd0" style="font-weight:bold;">仓库位置</td>
<td width="18%" align="center" class="stocktd0" style="font-weight:bold;">库存数量</td>
<td height="20" align="center" class="stocktd0" style="font-weight:bold;" colspan="2">产品最近一次出入库日期与操作记录</td>
</tr></HeaderTemplate> <ItemTemplate><tr >
<td align="center" class="stocktd&quo