zoukankan      html  css  js  c++  java
  • 导出excel

      public static  void  NPOIExportReport(  string [] titlearry, string  fileurl,DataTable dt)
            {
                FileStream fileStream = null;
                HSSFWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet("sheet1");
                try
                {
                        sheet = workbook.GetSheetAt(0);
                        IRow row1 = sheet.CreateRow(0);
    
                        //表头
                        for (int m= 0; m< dt.Columns.Count; m++)
                        {
                            ICell cell = row1.CreateCell(m);
                            cell.SetCellValue(titlearry[m]);
                        }
    
                        for (int i=0;i<dt.Rows.Count;i++)
                        {
                            IRow row = sheet.CreateRow(i + 1);
                            for(int t = 0; t < dt.Columns.Count; t++)
                               {
                                ICell cell = row.CreateCell(t);
                                cell.SetCellValue(dt.Rows[i][t].ToString());
                                }
                        }
    
                        fileStream = System.IO.File.Create(fileurl);
                        workbook.Write(fileStream);
                        fileStream.Close();
                        downloadfile(fileurl);
                }
                catch (Exception  ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (workbook != null)
                        workbook.Close();
                }
                
            /// <summary>
            /// http页面包头
            /// </summary>
            public static void downloadfile(string s_path)
            {
                System.IO.FileInfo file = new System.IO.FileInfo(s_path);
                HttpContext.Current.Response.ContentType = "application/ms-download";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
                HttpContext.Current.Response.WriteFile(file.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.End();
            }
    111111
  • 相关阅读:
    取随机数
    端口号
    cut命令
    渗透
    ssh免密登陆
    漏洞扫描
    信息收集1:DNSEUM命令
    Centos6与Centos7的区别
    HAPROXY+KEEPALIVED实现负载均衡
    this的用法
  • 原文地址:https://www.cnblogs.com/whl4835349/p/14435506.html
Copyright © 2011-2022 走看看