//调用
 public ActionResult GenerateExcel() {
            DataTable headerTable = new DataTable();
            headerTable.Columns.Add("Status_Id", Type.GetType("System.String"));
            headerTable.Columns.Add("Status_Name", Type.GetType("System.String"));
            headerTable.Columns.Add("Is_Now", Type.GetType("System.String"));
            headerTable.Columns.Add("Curr_User_Id", Type.GetType("System.String"));
            headerTable.Columns.Add("Flow_Id", Type.GetType("System.String"));
           
            DataRow headerRow = headerTable.NewRow();
            headerRow["Status_Id"] = "Status_Id";
            headerRow["Status_Name"] = "Status_Name";
            headerRow["Is_Now"] = "Is_Now";
            headerRow["Curr_User_Id"] = "Curr_User_Id";
            headerRow["Flow_Id"] = "Flow_Id";
            headerTable.Rows.Add(headerRow);

            DataTable contentTable = getDataByProc();

            generateExcelOP(headerTable, contentTable);
            ViewData["result"] = "Create successfully";
            return View("ExcelUpload");
        }

        //生成Excel

        public void generateExcelOP(DataTable headerTable,DataTable dt) {

            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet;

            //excel header info
            HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow;
            for(int i = 0 ;i < headerTable.Columns.Count;i++){
                HSSFCell headerCell = headerRow.CreateCell(i) as HSSFCell;
                headerCell.SetCellValue(headerTable.Rows[0][i].ToString());
                headerRow.Cells.Add(headerCell);
            }

            //excel content info
            for(int m =0 ;m < dt.Rows.Count;m++){
                HSSFRow row = sheet.CreateRow((m+1)) as HSSFRow;
                for (int n = 0; n < dt.Columns.Count;n++ )
                {
                    HSSFCell cell = row.CreateCell(n) as HSSFCell;
                    cell.SetCellValue(dt.Rows[m][n].ToString());
                    row.Cells.Add(cell);
                }
            }

            FileStream fs = new FileStream(@"D:\data\output.xls", FileMode.Create);
            workbook.Write(fs);
            fs.Flush();
            fs.Close();
            workbook.Clear();
        }

 

       //获取数据

        public DataTable getDataByProc() {
            DataTable dt = new DataTable();
            string callName = "GetAuthHistory";
            string devConn = System.Configuration.ConfigurationManager.ConnectionStrings["devConn"].ToString();
            using(SqlConnection conn = new SqlConnection(devConn)){
                SqlCommand command = new SqlCommand(callName, conn);
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter[] sps = {
                                     new SqlParameter("@currPage",SqlDbType.Int),
                                      new SqlParameter("@pageSize",SqlDbType.Int)
                                     };
                sps[0].Value = 1;
                sps[1].Value = 20;
               foreach(SqlParameter sp in sps){
                   command.Parameters.Add(sp);
               }
               SqlDataAdapter sda = new SqlDataAdapter();
               sda.SelectCommand = command;
               sda.Fill(dt);
            }

            return dt;
        }

最新文章

  1. 修改linux的hosts 后提示“无效的参数”
  2. Linux下的压缩和解压缩命令——gzip/gunzip
  3. Ubuntu系统下的第一个console程序
  4. JS网页加载进度条
  5. EChart 关于图标控件的简单实用
  6. PHP Array函数分类
  7. 可伸缩性/可扩展性(Scalable/scalability)
  8. [A Top-Down Approach][第一章 计算机网络和因特网]
  9. spark JavaDirectKafkaWordCount 例子分析
  10. Pow(x, n) leetcode
  11. 实例讲解js正则表达式的使用
  12. windows服务器修改登录密码
  13. 机器学习基石:14 Regularization
  14. 如何在element-ui table 取到对应的ID的最后一个对象
  15. Mybatis日志
  16. getParameter和getAttribute的区别
  17. 字符缓冲流BufferedWriter BufferedReader
  18. codeforces 1058D.Vasya and Triangle (gcd)
  19. 匈牙利算法--java
  20. django模板中的自定义过滤器

热门文章

  1. CSS:教程
  2. python中的缓存技术
  3. PAT_A1124#Raffle for Weibo Followers
  4. 剑指offer——71扑克牌中的顺子
  5. BigDecimal.divide方法
  6. react map循环数据 死循环
  7. poj 1905 图形推算+二分
  8. mongodb入门篇
  9. [LOJ#2732] 「JOISC 2016 Day 2」雇佣计划
  10. Hadoop&ndash;Task 相关