C # Simple way to export Excel using WinForm

Source: Internet
Author: User
This article describes a simple way for C # to export Excel using WinForm. Share to everyone for your reference, as follows:

Using Excel;

Introducing Excel.dll in the project

<summary>///export excel///</summary>///<param name= "sender" ></param>///<param name= "E" ></param>private void Btnexportexcel_click (object sender, EventArgs e) {DataTable dt =  This.dgvWaterTicket.DataSource;  if (dt = = null) {return; } if (dt.  Rows.Count = = 0) {return;  } excel.application xlapp = new Excel.Application ();    if (xlapp = = null) {MessageBox.Show ("Make sure your computer is installed Excel", "prompt Message", MessageBoxButtons.OK, Messageboxicon.error);  Return  } Xlapp.usercontrol = true;  Excel.Workbooks Workbooks = xlapp.workbooks; A new Workbook//workbook Workbook = workbooks is generated according to the template.  ADD ("D:\\aa.xls"); Excel.Workbook Workbook = workbooks.  ADD (Excel.XlWBATemplate.xlWBATWorksheet); Excel.Worksheet Worksheet = (excel.worksheet) workbook. worksheets[1];//get sheet1 if (worksheet = = null) {MessageBox.Show ("Make sure your computer has Excel installed", "prompt Message", MessageBoxButtons.OK, M    ESSAGEBOXICON.ERROR);  Return    } try {Excel.Range Range; Long totalcount = dt. Rows.coUnt    Long rowread = 0;    float percent = 0; Worksheet. Cells[1, 1] = frm. text;//the exported title worksheet.get_range (worksheet. Cells[1, 1], worksheet. cells[1, dt.]). MergeCells = true; Merge cells---Number of columns worksheet.get_range (worksheet. Cells[1, 1], worksheet. Cells[1, 3]). HorizontalAlignment = excel.xlvalign.xlvaligncenter;//Center alignment worksheet.get_range (worksheet. Cells[1, 3], worksheet. Cells[1, 3]).   ColumnWidth = 15; Column width worksheet.get_range (worksheet. Cells[1, 2], worksheet. Cells[1, 2]).   ColumnWidth = 15; Column width worksheet.get_range (worksheet. Cells[1, 1], worksheet. Cells[1, 1]).   ColumnWidth = 20; Column width//write field for (int i = 0; i < dt. Columns.count; i++) {worksheet. cells[2, i + 1] = dt. Columns[i].      ColumnName; Range = (excel.range) worksheet.      cells[2, i + 1]; Range.      Interior.ColorIndex = 15; Range.    Font.Bold = true; }//write value for (int r = 0; r < dt. Rows.Count; r++) {for (int i = 0; i < dt. Columns.count; i++) {worksheet. Cells[r + 3, i + 1] = dt.      Rows[r][i];      } rowread++;      Percent = ((float) (rowread))/totalcount;      System.Threading.Thread.Sleep (500); If the number of words is too large, the word wraps automatically. Worksheet. Cells[r+1, 4] for worksheet. cells[row, column] worksheet.get_range (worksheet. Cells[r + 3, 4], worksheet. Cells[r + 1, 4]).   Columns.wraptext = true; Line wrap worksheet.get_range (worksheet. Cells[r + 3, 4], worksheet. Cells[r + 3, 4]). Rows.autofit (); Automatically add high//this. Text = "Export data [" + percent.    ToString ("0.00") + "%] ..."; } range = worksheet.get_range (worksheet. Cells[2, 1], worksheet. Cells[dt. Rows.Count + 2, dt.    Columns.count]); Range. Borderaround (Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin,    Excel.XlColorIndex.xlColorIndexAutomatic, NULL); Range. Borders[excel.xlbordersindex.xlinsidehorizontal].    ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; Range. Borders[excel.xlbordersindex.xlinsidehorizontal].    LineStyle = Excel.XlLineStyle.xlContinuous; Range. Borders[excel.xlbordersindex.xlinsideHorizontal].    Weight = Excel.XlBorderWeight.xlThin; if (dt. Columns.count > 1) {range. Borders[excel.xlbordersindex.xlinsidevertical].      ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; Range. Borders[excel.xlbordersindex.xlinsidevertical].      LineStyle = Excel.XlLineStyle.xlContinuous; Range. Borders[excel.xlbordersindex.xlinsidevertical].    Weight = Excel.XlBorderWeight.xlThin;  } xlapp.visible = true; } catch {MessageBox.Show ("Failed to Out of Excel!  "," hint message ", MessageBoxButtons.OK, Messageboxicon.error);    } finally {System.Runtime.InteropServices.Marshal.ReleaseComObject (worksheet);    System.Runtime.InteropServices.Marshal.ReleaseComObject (workbook);    System.Runtime.InteropServices.Marshal.ReleaseComObject (workbooks);    System.Runtime.InteropServices.Marshal.ReleaseComObject (xlapp);    KillProcess ("Excel"); Gc. Collect ();//Forcibly Destroy}}

Read more about C # Simple ways to export Excel using WinForm related articles please follow topic.alibabacloud.com!

  • 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.