• NPIO 导出Execl


    步骤1:导入NOIO.dll    (我导入压缩包中的4.0)

    下载地址:http://npoi.codeplex.com/downloads/get/1572743   

    步骤二:粘贴代码(^  ....   ^)

        public class ExeclController : Controller
        {
            //
            // GET: /Execl/
            public ActionResult Index()
            {
                DataTable dt = new DataTable("AllVehSite");
                DataColumnCollection columns = dt.Columns;
                columns.Add("AcctId", typeof(System.Int32));
                columns.Add("SiteId", typeof(System.Int32));
                columns.Add("DbType", typeof(System.String));
                columns.Add("ConnStr", typeof(System.String));
             

                DataRow datarow = dt.NewRow();
                datarow["AcctId"] = 1;
                datarow["SiteId"] = 2;
                datarow["DbType"] = "2";
                datarow["ConnStr"] = "1";

                dt.Rows.Add(datarow);
                
                WriteExcel(dt, @"C:UsersAdministratorDesktopdatas111.xls");


                return View();
            }

            #region 导出Execl
            public static void WriteExcel(DataTable dt, string filePath)
            {
                if (!string.IsNullOrEmpty(filePath) && null != dt && dt.Rows.Count > 0)
                {
                    NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                    NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(dt.TableName);

                    NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
                    }
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
                        }
                    }
                    // 写入到客户端  
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        book.Write(ms);
                        using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                        {
                            byte[] data = ms.ToArray();
                            fs.Write(data, 0, data.Length);
                            fs.Flush();
                        }
                        book.Close();
                        book = null;
                    }
                }
            }
            #endregion  

          

        }

    步骤3:修改路径,Databable

  • 相关阅读:
    算法之递归(4) 应用
    算法之递归(1)
    [Async] [Series #1] 初识Async异步编程模型。
    CVE202142287/CVE202142278 复现
    易读文库下载器1.2版发布
    Sqlite.net 读取DateTime异常的解决方案
    QZFL 2.0.5 源代码
    Sqlite 管理工具 SQLiteDeveloper 及破解
    visio2010数据库正向工程生成数据库脚本
    什么是高内聚、低耦合?
  • 原文地址:https://www.cnblogs.com/j2ee-web-01/p/7359568.html
Copyright © 2020-2023  润新知