• .net上传文件,利用npoi读取文件信息到datatable里


    整理代码,.net上传文件,利用npoi读取文件到datatable里,使用了FileUpload控件,代码如下:

     1 protected void Button1_Click(object sender, EventArgs e)
     2 {
     3     try
     4     {
     5         #region 上传文件
     6         if (FileUpload1.HasFile)//判断是否存在上传文件
     7         FileUpload1.SaveAs(Server.MapPath("~/") + FileUpload1.FileName);
     8         else
     9         {
    10             ScriptManager.RegisterClientScriptBlock(this, GetType(), "aaa", "alert('上传文件不存在或上传文件为空,请检查文件');", true);
    11             return;
    12         }
    13         #endregion
    14         #region 读取文件
    15         DataTable dt = null;
    16         string fileEx = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
    17         if (fileEx == "xls")
    18         {
    19             dt = IO.ImportExcelFile(Server.MapPath("~/") + FileUpload1.FileName);
    20         }
    21         else if (fileEx == "xlsx")
    22         {
    23             dt = IO.ImportExcelFilexlsx(Server.MapPath("~/") + FileUpload1.FileName);
    24         }
    25         if (dt.Rows.Count < 1)
    26         {
    27             ScriptManager.RegisterClientScriptBlock(this, GetType(), "aaa", "alert('上传文件为空,请检查文件内容');", true);
    28             return;
    29         }
    30         #endregion
    31     }
    32     catch (Exception ex)
    33     {
    34         ScriptManager.RegisterClientScriptBlock(this, GetType(), "aaa", "alert('系统异常," + ex + "');", true);
    35     }
    36 }

    npoi读取文件的方法:

     1 public static DataTable ImportExcelFile(string filePath)
     2 {
     3     HSSFWorkbook hssfworkbook;
     4     #region//初始化信息
     5     try
     6     {
     7         using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
     8         {
     9             hssfworkbook = new HSSFWorkbook(file);
    10         }
    11     }
    12     catch (Exception e)
    13     {
    14         throw e;
    15     }
    16     #endregion
    17     NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
    18     System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    19     DataTable dt = new DataTable();
    20     for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
    21     {
    22         dt.Columns.Add(sheet.GetRow(0).GetCell(j).ToString());
    23     }
    24     while (rows.MoveNext())
    25     {
    26         HSSFRow row = (HSSFRow)rows.Current;
    27         if (row.RowNum != 0)
    28         {
    29             DataRow dr = dt.NewRow();
    30             for (int i = 0; i < row.LastCellNum; i++)
    31             {
    32                 NPOI.SS.UserModel.ICell cell = row.GetCell(i);
    33                 if (cell == null)
    34                 {
    35                     dr[i] = null;
    36                 }
    37                 else
    38                 {
    39                     dr[i] = cell.ToString();
    40                 }
    41             }
    42             dt.Rows.Add(dr);
    43         }
    44     }
    45     return dt;
    46 }
    47 public static DataTable ImportExcelFilexlsx(string filePath)
    48 {
    49     XSSFWorkbook hssfworkbook;
    50     #region//初始化信息
    51     try
    52     {
    53         using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    54         {
    55             hssfworkbook = new XSSFWorkbook(file);
    56         }
    57     }
    58     catch (Exception e)
    59     {
    60         throw e;
    61     }
    62     #endregion
    63     NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
    64     System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
    65     DataTable dt = new DataTable();
    66     for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
    67     {
    68         dt.Columns.Add(sheet.GetRow(0).GetCell(j).ToString());
    69     }
    70     while (rows.MoveNext())
    71     {
    72         XSSFRow row = (XSSFRow)rows.Current;
    73         if (row.RowNum != 0)
    74         {
    75             DataRow dr = dt.NewRow();
    76             for (int i = 0; i < row.LastCellNum; i++)
    77             {
    78                 NPOI.SS.UserModel.ICell cell = row.GetCell(i);
    79                 if (cell == null)
    80                 {
    81                     dr[i] = null;
    82                 }
    83                 else
    84                 {
    85                     dr[i] = cell.ToString();
    86                 }
    87             }
    88             dt.Rows.Add(dr);
    89         }
    90     }
    91     return dt;
    92 }
  • 相关阅读:
    Taro api封装
    taro中如何定义全局变量
    移动端悬浮框可移动,可回弹,Vue and React
    VS2015 安装mvc4安装包以及vs2010 sp1后导致Razor语法失效代码不高亮(能正常运行)/视图页面无法智能提示(.cshtml)解决办法
    C#6.0新特性的尝试
    Visual Studio Code 添加设置代码段(snippet)
    AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
    [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler
    [Asp.net mvc]实体更新异常:存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。
    win8.1右键新建菜单添加新建php文件
  • 原文地址:https://www.cnblogs.com/Lvkang/p/9358823.html
Copyright © 2020-2023  润新知