Unity C # Call SaveFileDialog to save the Excel file,
This article is original, reproduced please indicate the source: http://www.cnblogs.com/AdvancePikachu/p/6893934.html
This document describes how to convert data into an Excel file and call the SaveFileDialog window to save the file.
First, you must reference several Plugins: System. Windows. Forms (SaveFileDialog window) NOPI (Data Storage of Excel files ).
Code for calling the SaveFileDialog window:
1 public static void OpenDialog (Action <Stream> onSave) {2 using (SaveFileDialog saveFile = new SaveFileDialog () {3 saveFile. title = "save file"; 4 saveFile. filter = "Excel files (*. xls) | *. xls | All files (*. *) | *. * "; 5 saveFile. initialDirectory = UnityEngine. application. dataPath; 6 if (saveFile. showDialog () = DialogResult. OK) {7 using (Stream s = saveFile. openFile () {8 if (onSave! = Null) 9 onSave (s); 10} 11 12 string Savepath = Path. GetDirectoryName (saveFile. FileName); 13 14 Process. Start (Savepath); 15} 16} 17}
To save other types of files, you can change the above Code to what you want.
As follows:
Unfortunately, the window is in English. I checked a lot of information and found that there was a problem with the Chinese language. Therefore, if some friends know how to make the Chinese language, remember to contact AdvancePikachu.
The following is the data storage code for Excel files:
1 public void WriteToStream (Stream s) 2 {3 IWorkbook workbook = new HSSFWorkbook (); 4 ISheet sheet = workbook. createSheet (); 5 6 IRow row = sheet. createRow (0); // parameter 0 indicates Row 7 8 string [] firstRow = new string [] 9 {10 "ID", 11 "gender ", 12 "blog" 13}; 14 15 for (int I = 0; I <firstRow. length; I ++) 16 {17 ICell cell = row. createCell (I); 18 cell. setCellValue (firstRow [I]); 19} 20 21 IRow row2 = sheet. createRow (1); 22 23 string [] secondRow = new string [] 24 {25 "AdvancePikachu", 26 "male", 27 "http://www.cnblogs.com/AdvancePikachu/" 28 }; 29 30 for (int I = 0; I <secondRow. length; I ++) 31 {32 ICell cell = row2.CreateCell (I); 33 cell. setCellValue (secondRow [I]); 34} 35 36 workbook. write (s); 37}
You can set the stored data and columns as follows:
Of course, I will also share the method for calling the first script,
The Code is as follows:
1 void save() 2 { 3 SaveDialog.OpenDialog (saveExcel); 4 } 5 6 void saveExcel(Stream s) 7 { 8 ExcelWrite ew = new ExcelWrite (); 9 ew.WriteToStream (s);10 }
Well, the learning of saving Excel files by calling SaveFileDialog has come to an end.