Unity C # Call SaveFileDialog to save the Excel file,

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.