C # Methods for using WinForm to simply export Excel _c# tutorials

Source: Internet
Author: User

The example in this article describes how C # uses WinForm to simply export Excel. Share to everyone for your reference, specific as follows:

Using Excel;

Introducing Excel.dll into 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.Da
  Tasource;
  if (dt = null) {return; } if (dt.
  Rows.Count = = 0) {return;
  } excel.application xlapp = new Excel.Application ();
    if (xlapp = = null) {MessageBox.Show ("Please make sure your computer is installed Excel", "Prompt info", MessageBoxButtons.OK, Messageboxicon.error);
  Return
  } Xlapp.usercontrol = true;
  Excel.Workbooks workbooks = xlapp.workbooks; According to the template produced a new workbook//workbook workbook = workbooks.
  ADD ("D:\\aa.xls"); Excel.Workbook workbook = workbooks.
  ADD (Excel.XlWBATemplate.xlWBATWorksheet); excel.worksheet worksheet = (excel.worksheet) workbook. Worksheets[1];//gets Sheet1 if (worksheet = null) {MessageBox.Show ("Please make sure your computer has Excel installed", "prompt info", MessageBoxButtons.OK
    , Messageboxicon.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); Wraps automatically if the number of words is too large. 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; Auto wrap worksheet.get_range (worksheet. Cells[r + 3, 4], worksheet. Cells[r + 3, 4]). Rows.autofit (); Automatic addition of 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 excel!")
  "," Cue Information ", 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 # Interested readers can view the site topics: "C # Operations Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # An introductory course on object-oriented programming and a summary of thread usage tips for C # programming

I hope this article will help you with C # programming.

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.