zoukankan      html  css  js  c++  java
  • 将数据写入EXCEL多个表

    首先在项目中添加引用-Microsoft.Office.Interop.Excel

    代码
     1 using Excel = Microsoft.Office.Interop.Excel;
     2 using System.Reflection;
     3 using System.Text;
     4 using System.Collections.Generic;
     5 
     6 public static void creat_excel(List<DataTable> d)
     7     {
     8         Excel.Application ex = new Excel.Application();
     9         Excel.Workbook workbook = ex.Workbooks._Open(HttpContext.Current.Server.MapPath("y.xls"), Missing.Value,
    10             Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    11             Missing.Value, Missing.Value, Missing.Value);
    12         write_excel(ex, d[0], 18);
    13         write_excel(ex, d[1], 23);
    14         ex.Visible = false;
    15         
    16        
    17         ex.DisplayAlerts = false;
    18         ex.AlertBeforeOverwriting = false;
    19         
    20         string sv = HttpContext.Current.Server.MapPath("X.xls");
    21         workbook.SaveCopyAs(sv);
    22         workbook.Close(falsenullnull);
    23 
    24         ex.Quit();
    25         ex=null;
    26         GC.Collect();
    27         downloadFile(sv);
    28 
    29     }
    30     private  static void write_excel(Excel.Application ex,  DataTable dt,int sheetNum, int rowNum)
    31     {
    32         Excel._Worksheet wSheet = ex.Worksheets[sheetNum] as Excel.Worksheet;
    33         
    34         for (int i = 1; i < 3; i++)
    35         {
    36             for (int m = 0; m < dt.Columns.Count; m++)
    37             {
    38                 if (i == 2 && m == 0)
    39                     wSheet.Cells[rowNum + 11= "合计";
    40                 else
    41                     wSheet.Cells[rowNum + i - 1, m + 1= dt.Rows[0][m].ToString();
    42             }
    43         }
    44 
    45     }
    46 
    47  public static void downloadFile(string sv)
    48     {
    49         FileInfo file = new FileInfo(sv);
    50         HttpContext.Current.Response.Clear();
    51         HttpContext.Current.Response.HeaderEncoding = Encoding.GetEncoding("GB2312");
    52         HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
    53         HttpContext.Current.Response.ContentType = "application/ms-excel";
    54         HttpContext.Current.Response.Charset = "GB2312";
    55         HttpContext.Current.Response.AppendHeader("Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(sv) + "");
    56         // 添加头信息,指定文件大小,让浏览器能够显示下载进度 
    57         HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
    58 
    59         HttpContext.Current.Response.TransmitFile(file.FullName);
    60         HttpContext.Current.Response.Flush();
    61         HttpContext.Current.Response.Close();
    62         file.Delete();
    63     }

    最后输出到浏览器并删除临时EXCEL文件

  • 相关阅读:
    数据结构之利用递归解决八皇后问题
    SpringBoot注解大全
    shiro框架中出现的错误There was an unexpected error (type=Unauthorized, status=401).
    shiro权限控制
    shiro的整合“心路历程”
    _T宏的使用
    根据文件的路径 分割文件名 文件后缀
    将选中的图片文件以图片形式 (显示在对话框内)或(直接发送出去)
    对话框内粘贴图片文件会直接发送,改为粘贴图片后直接将图片显示在对话内。
    数据库按日期查询,右侧日期内的数据无法现实问题。
  • 原文地址:https://www.cnblogs.com/wishbay/p/1657309.html
Copyright © 2011-2022 走看看