/// <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"))) { } }