Browse Source

文件生成BUG解决

callm 5 years ago
parent
commit
3821290b13
1 changed files with 12 additions and 5 deletions
  1. 12 5
      UAS-出货标签管理(贸易版)/PublicMethod/ExcelHandler.cs

+ 12 - 5
UAS-出货标签管理(贸易版)/PublicMethod/ExcelHandler.cs

@@ -47,9 +47,13 @@ namespace UAS_LabelMachine
             ms = DataTableToExcel2(firstsdt, dt, Type, FileName, PageSize, conditionbox);
             //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
             string filePath = @FolderPath + "\\" + FileName + ".xls";
-            if (File.Exists(filePath))
+            int counter = 1;
+            string filename = FileName + ".xls";
+            while (File.Exists(filePath))
             {
-                filePath = @FolderPath + "\\" + FileName + "(1)" + ".xls";
+                filename = string.Format("{0}({1}){2}", FileName, counter, ".xls"); //文件名+(count)+后缀
+                filePath = Path.Combine(FolderPath, filename);  //保存路径
+                counter++; //count+1
             }
             FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
             byte[] data = ms.ToArray();
@@ -71,9 +75,12 @@ namespace UAS_LabelMachine
             ms = DataTableToExcel1(firstsdt, dt, Type, FileName, PageSize, conditionbox);
             //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
             string filePath = @FolderPath + "\\" + FileName + ".xls";
-            if (File.Exists(filePath))
-            {
-                filePath = @FolderPath + "\\" + FileName + "(1)" + ".xls";
+            int counter = 1;
+            string filename = FileName + ".xls";
+            while (File.Exists(filePath)) {
+                filename = string.Format("{0}({1}){2}", FileName , counter, ".xls"); //文件名+(count)+后缀
+                filePath = Path.Combine(FolderPath, filename);  //保存路径
+                counter++; //count+1
             }
             FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
             byte[] data = ms.ToArray();