How to export files to Word, Excel, and text files in WinForm
I haven't written an article for a long time. Let me share with you a small export file method I have used in my recent program. Everyone is welcome to arrange bricks. Thank you~ I won't talk nonsense, just enter the code:
using System; using ; using ; using ; using ; using ; using ; using ; using ; using ; namespace { /// <summary> /// Export the content to a file class./// </summary> /// <remarks> /// Author: SunYujing/// Date: 2011-12-18/// </remarks> public class ExportFile { /// <summary> /// Method to store strings as files in word document format (multi-threaded)./// </summary> /// <param name="strText">The content of the string to be saved. </param> public static void SaveAsWord(string p_str) { Thread thread = new Thread(SaveAsWordFile); (); (p_str); } /// <summary> /// Method to store strings as files in txt format (multi-threaded)./// </summary> /// <param name="p_str"></param> public static void SaveAsTxt(string p_str) { Thread thread = new Thread(SaveAsTxtFile); (); (p_str); } /// <summary> /// Export data table data to Excel (multi-threaded)./// </summary> public static void SaveAsExcel( dataTable) { if (dataTable == null) { ("Please specify the data table to export first"); return; } Thread thread = new Thread(SaveAsExcelTableFile); (); (dataTable); } /// <summary> /// Export dataset data to Excel (multi-threaded)./// </summary> public static void SaveAsExcel( dataSet) { if (dataSet == null) { ("Please specify the dataset to export first"); return; } Thread thread = new Thread(SaveAsExcelSetFile); (); (dataSet); } /// <summary> ///Storage strings as file in word document format./// </summary> /// <param name="strtext">The content of the string to be saved. </param> private static void SaveAsWordFile(object strtext) { SaveFileDialog sfd = new SaveFileDialog(); = "Please select the file storage path"; = "Export data"; = "Worddocument(*.doc)|*.doc"; if (() != ) { return; } string FileName = (); if (!(".doc")) { FileName += ".doc"; } if ((()).Length <= 5) { ("File saving failed, the file name cannot be empty!"); return; } try { DateTime start = ; ("Saving the file, please wait..."); word = new (); ._Document doc; object nothing = ; doc = (ref nothing, ref nothing, ref nothing, ref nothing); = (); object myfileName = FileName; //Save the content of the WordDoc document object as a doc document (ref myfileName, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing); //Close WordDoc document object (ref nothing, ref nothing, ref nothing); //Close WordApp component object (ref nothing, ref nothing, ref nothing); (); DateTime end = ; TimeSpan ts = end - start; ("The file is saved successfully, it takes time" + ()); } catch ( ex) { (); } } /// <summary> ///Storage strings as txt document format file./// </summary> /// <param name="strtext">The content of the string to be saved. </param> private static void SaveAsTxtFile(object strtext) { SaveFileDialog sfd = new SaveFileDialog(); = "Please select the file storage path"; = "Export data"; = "文本document(*.txt)|*.txt"; if (() != ) { return; } string FileName = (); if (!(".txt")) { FileName += ".txt"; } if ((()).Length <= 5) { ("File saving failed, the file name cannot be empty!"); return; } try { DateTime start = ; StreamWriter sw = new StreamWriter(FileName, false); (()); (); (); DateTime end = ; TimeSpan ts = end - start; ("The file is saved successfully, it takes time" + ()); } catch (Exception ex) { (); } } /// <summary> ///Storage data as an Excel file./// </summary> /// <param name="p_dt">The data table to be saved. </param> private static void SaveAsExcelTableFile(object p_dt) { dt = ()p_dt; if ( == 0) { ("No data to save"); return; } SaveFileDialog sfd = new SaveFileDialog(); = "Please select the file storage path"; = "Export data"; = "Exceldocument(*.xls)|*.xls"; if (() != ) { return; } string FileName = (); if (!(".xls")) { FileName += ".xls"; } if ((()).Length <= 5) { ("File saving failed, the file name cannot be empty!"); return; } if ( != "") { excelApp = new (); if (excelApp == null) { ("Unable to create Excel object, maybe your machine does not have Excel installed"); return; } else { ("Exporting data, please wait..."); DateTime start = ; workbooks = ; workbook = (); worksheet = (Worksheet)[1]; for (int col = 1; col <= ; col++) { [1, col] = [col - 1].(); } for (int i = 0; i < ; i++) { for (int j = 0; j < ; j++) { [i + 2, j + 1] = [i][j].ToString(); } } = true; (); //Release resources (worksheet); worksheet = null; (workbook); workbook = null; (); (workbooks); workbooks = null; (); (excelApp); excelApp = null; //Use garbage collection to close the process (); DateTime end = ; int iTimeSpan = ( - ) * 60 + ( - ); ("Data export is completed, it takes time" + () + "Second"); } } } /// <summary> ///Save the dataset as an Excel file./// </summary> /// <param name="p_ds">The dataset to be exported. </param> private static void SaveAsExcelSetFile(object p_ds) { ds = ()p_ds; if (ds == null || == 0) { ("No data to save"); return; } SaveFileDialog sfd = new SaveFileDialog(); = "Please select the file storage path"; = "Export data"; = "Exceldocument(*.xls)|*.xls"; if (() != ) { return; } string FileName = (); if (!(".xls")) { FileName += ".xls"; } if ((()).Length <= 5) { ("File saving failed, the file name cannot be empty!"); return; } if ( != "") { excelApp = new (); if (excelApp == null) { ("Unable to create Excel object, maybe your machine does not have Excel installed"); return; } else { ("Exporting data, please wait..."); DateTime start = ; workbooks = ; workbook = (); worksheet = null; object objMissing = ; for (int m = 0; m < ; m++) { dt = [m]; worksheet = (Worksheet); = ; for (int col = 1; col <= ; col++) { [1, col] = [col - 1].(); } for (int i = 1; i <= ; i++) { for (int j = 1; j <= ; j++) { [i + 1, j] = [i - 1][j - 1].ToString(); } } if (m < - 1) { (objMissing, objMissing, 1, ); } } = true; (); //Release resources (worksheet); worksheet = null; (workbook); workbook = null; (); (workbooks); workbooks = null; (); (excelApp); excelApp = null; (); DateTime end = ; int iTimeSapn = ( - ) * 60 + ( - ); ("Data export is completed, it takes time" + (iTimeSapn / 60).ToString() + "point" + (iTimeSapn % 60).ToString() + "Second"); } } } } }
Related Articles
Asynchronous is a detailed explanation of 4 implementation methods in C#
This article mainly introduces the relevant knowledge of asynchronous implementation methods in C# in detail. The sample code in the article is explained in detail and has certain reference value. Friends in need can follow the editor to learn it.2024-03-03Detailed explanation of how to implement Word conversion TXT in C#
This article mainly introduces in detail how to use C# to realize the function of Word conversion TXT. The sample code in the article is explained in detail, which is of some help to our learning C#. Interested friends can follow the editor to learn about it.2022-12-12How to measure microsecond accuracy using C# Stopwatch
This article mainly introduces how to use C# Stopwatch to measure microsecond accuracy, helping everyone better understand and learn how to use C#. Interested friends can learn about it.2021-03-03Use C# to implement WeChat automation function
This article mainly introduces the use of C# to realize WeChat automation. This article introduces you very detailed through the example code, which has certain reference value for your study or work. Friends who need it can refer to it.2023-08-08Implementation of C# scroll subtitles
This article mainly introduces the implementation method of C# scroll subtitles, mainly talks about the application method of functions, which is very practical. Friends who need it can refer to it.2014-10-10c# Get quarterly time instance code (first day of quarter)
This article mainly introduces the functions of C# obtaining quarterly time: the first day of the quarter, the last day of the quarter, etc. Please refer to it and use it.2013-12-12Delegate in C#
This article introduces the delegation Delegate in C#, and the article introduces it in very detailed through sample code. It has certain reference value for everyone's study or work. Friends who need it can refer to it.2022-05-05C# uses Win32 API to implement process injection into wechat
This article mainly introduces C# using Win32 API to implement the process of injecting into wechat. This article introduces you very detailedly and has certain reference value for your study or work. Friends who need it can refer to it.2023-09-09How to use MSMQ in C#
This article mainly introduces the method of using MSMQ in C# to help everyone better understand and use c#. Interested friends can learn more2021-01-01Share several unknown Visual Studio coding skills in C#
I have been using Visual Studio for many years and only discovered this coding technique today. I am really ashamed. I can share it and it can be considered a way to attract jade. Friends who need it can refer to it.2012-11-11