• 删除Excel空列


     /// <summary>
            /// 删除Excel空列
            /// </summary>
            /// <param name="excelPath">Excel路径</param>
            /// <param name="columns">是空行但要保留的列</param>
            /// <returns></returns>
            public static void DeleteNullColumn(string excelPath)
            {
                //用的时候就拿到Main方法里去
                //DirectoryInfo dirInfo = new DirectoryInfo(@"C:Users11572Desktop木板年画元数据(依模板 供数据加工使用)");
    
                //foreach (var item in dirInfo.GetFiles())
                //{
                //    DeleteNullColumn(item.FullName);
                //}
    
                //必须要的列
                string[] colums = new string[]{
                    "aaa","bbb","ccc"
                };
                DataTable dataTable = ExcelUtility.ExcelToDataTable(excelPath, 0, true);
    
                List<string> removeColumn = new List<string>();
                foreach (DataColumn column in dataTable.Columns)
                {
                    int columLenght = 0;
                    foreach (DataRow row in dataTable.Rows)
                    {
                        columLenght += row[column.ColumnName].ToString().Length;
                        if (columLenght > 0)
                        {
                            break;
                        }
                    }
                    if (columLenght == 0)
                    {
                        removeColumn.Add(column.ColumnName);
                    }
                }
                foreach (var col in colums)
                {
                    removeColumn.Remove(col);
                }
                foreach (var col in removeColumn)
                {
                    dataTable.Columns.Remove(col);
                }
    
                dataTable.Columns["xx"].ColumnName = "xx";
                dataTable.Columns["xx"].ColumnName = "xx";
    
                if (ExcelUtility.DataTableToExcel(dataTable, Path.Combine(Path.GetDirectoryName(excelPath), "新建文件夹", Path.GetFileNameWithoutExtension(excelPath) + ".xls")))
                {
    
                }
            }
  • 相关阅读:
    java.io.file
    连线小游戏
    发票类型区分的正则表达式(仅区分普票专票)
    mybatis: No enum constant org.apache.ibatis.type.JdbcType."VARCHAR"
    bootstrap inputfile 使用-上传,回显
    微积分极限中一例
    oracle 查看表结构语句
    redis无法连接
    项目配置shiro原缓存注解失效
    bug 找不到或无法加载主类main.java.*
  • 原文地址:https://www.cnblogs.com/mi21/p/11282544.html
Copyright © 2020-2023  润新知