C # How to export and import a complete example of Excel/Doc in winform [view on the Internet],

Source: Internet
Author: User

C # How to export and import a complete example of Excel/Doc in winform [view on the Internet],

I have never used winform to export and import data. Today I went online to Baidu. Result ---

So I should write it myself. If you have done web before, you can do it half by half.

1 Add reference: Aspose. Cells. dll (you can download the toolkit from the Internet. For more information about its operations, see the document "Aspose. Cells operation instructions for downloading Aspose C # export an Excel instance. You may ignore it for the moment .)

Aspose. Cells. dll and http://item.taobao.com/auction/item_detail-0db2-f9b1d99e6cb27d78ae9e6373a1529633.htm

It can be used even if the office is not installed. This is a powerful tool.

2. Compile Excel operations

Using System;
Using System. Collections. Generic;
Using System. Text;
Using Aspose. Cells;
Using System. Data;

Public class AsposeExcel
{

Private string outFileName = "";
Private string fullFilename = "";
Private Workbook book = null;
Private Worksheet sheet = null;


Public AsposeExcel (string outfilename, string tempfilename) // Number of exported Constructors
{
OutFileName = outfilename;
Book = new Workbook ();
// Book. Open (tempfilename); templates are not used here.
Sheet = book. Worksheets [0];
}
Public AsposeExcel (string fullfilename) // number of imported Constructors
{
FullFilename = fullfilename;
// Book = new Workbook ();
// Book. Open (tempfilename );
// Sheet = book. Worksheets [0];
}

Private void AddTitle (string title, int columnCount)
{
Sheet. Cells. Merge (0, 0, 1, columnCount );
Sheet. Cells. Merge (1, 0, 1, columnCount );

Cell cell1 = sheet. Cells [0, 0];
Cell1.PutValue (title );
Cell1.Style. HorizontalAlignment = TextAlignmentType. Center;
Cell1.Style. Font. Name = "";
Cell1.Style. Font. Size = 14;
Cell1.Style. Font. IsBold = true;

Cell cell2 = sheet. Cells [1, 0];
Cell1.PutValue ("query time:" + DateTime. Now. ToLocalTime ());
Cell2.SetStyle (cell1.Style );
}

Private void AddHeader (DataTable dt)
{
Cell cell = null;
For (int col = 0; col <dt. Columns. Count; col ++)
{
Cell = sheet. Cells [0, col];
Cell. PutValue (dt. Columns [col]. ColumnName );
Cell. Style. Font. IsBold = true;
}
}

Private void AddBody (DataTable dt)
{
For (int r = 0; r <dt. Rows. Count; r ++)
{
For (int c = 0; c <dt. Columns. Count; c ++)
{
Sheet. Cells [r + 1, c]. PutValue (dt. Rows [r] [c]. ToString ());
}
}
}
// Export ------------ this method will be used in the next article
Public Boolean DatatableToExcel (DataTable dt)
{
Boolean yn = false;
Try
{
// Sheet. Name = sheetName;

// AddTitle (title, dt. Columns. Count );
// AddHeader (dt );
AddBody (dt );

Sheet. AutoFitColumns ();
// Sheet. AutoFitRows ();

Book. Save (outFileName );
Yn = true;
Return yn;
}
Catch (Exception e)
{
Return yn;
// Throw e;
}
}

Public DataTable ExcelToDatatalbe () // Import
{
Workbook book = new Workbook ();
Book. Open (fullFilename );
Worksheet sheet = book. Worksheets [0];
Cells cells = sheet. Cells;
// Obtain the data in excel and save it to a able.
DataTable dt_Import = cells. ExportDataTableAsString (0, 0, cells. MaxDataRow + 1, cells. MaxDataColumn + 1, false );
// Dt_Import.
Return dt_Import;
}
}

3. compile the data export method in the export button event:

Private void bt_excel_out_Click (object sender, EventArgs e)
{

SaveFileDialog saveFileDialog1 = new SaveFileDialog (); // If you pull it out of the dialog box directly, you don't need to use new.

// Set the file type
SaveFileDialog1.Filter = "Export Excel (*. xls) | *. xls | Word (*. doc) | *. doc ";
SaveFileDialog1.FilterIndex = 2;
SaveFileDialog1.RestoreDirectory = true;
SaveFileDialog1.CreatePrompt = true;
SaveFileDialog1.Title = "export file storage path ";
// SaveFileDialog1.ShowDialog ();
// String strName = saveFileDialog1.FileName;

// Set the display sequence of default file types
// SaveFileDialog1.FilterIndex = 2;

// Whether to remember the last opened directory in the SAVE dialog box
SaveFileDialog1.RestoreDirectory = true;

// Click the Save button to enter
If (saveFileDialog1.ShowDialog () = DialogResult. OK)
{
// Obtain the file path
String localFilePath = saveFileDialog1.FileName. ToString ();

// Get the file name without path
String fileNameExt = localFilePath. Substring (localFilePath. LastIndexOf ("\") + 1 );

// Obtain the file path without the file name
String FilePath = localFilePath. Substring (0, localFilePath. LastIndexOf ("\\"));

// Add the time before the file name
String newFileName = DateTime. Now. ToString ("yyyyMMdd") + fileNameExt;


SaveFileDialog1.FileName = FilePath + "\" + newFileName;
System. IO. FileStream fs = (System. IO. FileStream) saveFileDialog1.OpenFile (); // output file

// The following shows how to export data to doc and export data to excel. For more information, see the next article.AsposeExcel
Class is not used yet, that is exactly what is used to export to excel)
StreamWriter writer = new StreamWriter (fs );
Writer. Write ("tttt"); // The data you want to export to the Word document.
Writer. Flush ();
Writer. Close ();
Fs. Close ();


}


}


C :\

Yes

Refer to this to clean up the C drive:
1. Disable System Restoration: My computer properties/System Restoration/disable System Restoration on all disks, but I will not be able to use system restoration in the future!
2. Disable System sleep: Control Panel/Power Supply/sleep/remove the check before starting system sleep
3. move the virtual memory, my computer properties/advanced/performance/settings/advanced/change/select the C disk, that is, the system disk, select the no-score page, and then set the virtual memory to its disk, A disk with more disk space remaining, such as D, E, and F. set to 1.5 ~ of memory ~ 2.5 times. The size can be set to the same!
5. Clear temporary IE folders, internet Options, and delete temporary and offline files.
6. delete system logs and program logs, my computer/control panel/management tools/Computer Management/Event Viewer/application, right-click/clear events, and clear system logs in sequence
7. Clear system cache: 2000 all files in the system: C: \ WINNT \ system32 \ dllcache
The XP system is: C: \ windows \ system32 \ dllcache all files under the system cache (open my computer/tool/file and Folder Options/hide the protected system file hook off to hide all files on the hook) ). You can also run the sfc.exe/purgecache command to automatically delete the file.
8. Clear the recycle bin
9. delete the files under c: \ windows \ SoftwareDistribution \ Download (the files downloaded when the system is updated are useless if you have installed the updates)
10. Delete all directories under c: \ windows \ RegisteredPackages
11. Delete all Files under C: \ WINDOWS \ Downloaded Program Files
12. view the hidden files that are known to be protected by the system in my computer folder option, and check all the files.
13. Delete c: \ windows \ All files with $8882305 $ (backup files after system update)

Zhidao.baidu.com/question/11035955.html
Zhidao.baidu.com/question/12223613.html
Zhidao.baidu.com/question/14874715.html
... The remaining full text>

C :\

Yes

Refer to this to clean up the C drive:
1. Disable System Restoration: My computer properties/System Restoration/disable System Restoration on all disks, but I will not be able to use system restoration in the future!
2. Disable System sleep: Control Panel/Power Supply/sleep/remove the check before starting system sleep
3. move the virtual memory, my computer properties/advanced/performance/settings/advanced/change/select the C disk, that is, the system disk, select the no-score page, and then set the virtual memory to its disk, A disk with more disk space remaining, such as D, E, and F. set to 1.5 ~ of memory ~ 2.5 times. The size can be set to the same!
5. Clear temporary IE folders, internet Options, and delete temporary and offline files.
6. delete system logs and program logs, my computer/control panel/management tools/Computer Management/Event Viewer/application, right-click/clear events, and clear system logs in sequence
7. Clear system cache: 2000 all files in the system: C: \ WINNT \ system32 \ dllcache
The XP system is: C: \ windows \ system32 \ dllcache all files under the system cache (open my computer/tool/file and Folder Options/hide the protected system file hook off to hide all files on the hook) ). You can also run the sfc.exe/purgecache command to automatically delete the file.
8. Clear the recycle bin
9. delete the files under c: \ windows \ SoftwareDistribution \ Download (the files downloaded when the system is updated are useless if you have installed the updates)
10. Delete all directories under c: \ windows \ RegisteredPackages
11. Delete all Files under C: \ WINDOWS \ Downloaded Program Files
12. view the hidden files that are known to be protected by the system in my computer folder option, and check all the files.
13. Delete c: \ windows \ All files with $8882305 $ (backup files after system update)

Zhidao.baidu.com/question/11035955.html
Zhidao.baidu.com/question/12223613.html
Zhidao.baidu.com/question/14874715.html
... The remaining full text>

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.