C # Office development,

Source: Internet
Author: User

Reprinted: C # Office development,

Address: http://blog.sina.com.cn/s/blog_604fb7ae0100x2s7.html

 

The office automation system of small and medium-sized enterprises must have the function of connecting to Microsoft Office software, such as importing data to workbooks and Word. C #. NET provides powerful functions in Office, as long as you import Microsoft. office. interop. excel namespace and the class under this namespace can be called in the program Excel, Word.

(1) Excel Development

First import the Microsoft. Office. Interop. Excel namespace

Required class

_ Application excel = new ApplicationClass (); // instantiate an object

Int rowIndex = 6;
Int colIndex = 0;

_ Workbook xBk;
_ Worksheet xSt;

XBk = excel. Workbooks. Add (true );
XSt = (Microsoft. Office. Interop. Excel. _ Worksheet) xBk. ActiveSheet;

// Obtain the column title
For (int I = 0; I <dgvYingShouAmount. Columns. Count; I ++ ){
ColIndex ++;
Excel. Cells [rowIndex, colIndex] = Convert. ToString (dgvYingShouAmount. Columns [I]. HeaderText );
}

// Obtain the data in the table

For (int I = 0; I <dgvYingShouAmount. Rows. Count; I ++ ){
RowIndex ++;
ColIndex = 0;
For (int j = 0; j <dgvYingShouAmount. Columns. Count; j ++ ){
ColIndex ++;

Excel. Cells [rowIndex, colIndex] = Convert. ToString (dgvYingShouAmount. Rows [I]. Cells [j]. Value );
XSt. get_Range (excel. Cells [rowIndex, colIndex], excel. Cells [rowIndex, colIndex]). HorizontalAlignment = XlHAlign. xlHAlignCenter;
}
}

Excel. Cells [1, 1] = "*** Company Limited ";
Excel. Cells [2, 1] = "Address ";
Excel. Cells [3, 1] = "fax ";
Excel. Cells [4, 1] = "customer statement ";
Excel. cells [5, 1] = "Operation Date:" + dtpStartDate. value. toString ("yyyy-MM-dd") + "/" + dtpEndDate. value. toString ("yyyy-MM-dd ");

//
// Set the title format of the entire report
//
XSt. get_Range (excel. Cells [1, 1], excel. Cells [1, 1]). Font. Bold = true;
XSt. get_Range (excel. Cells [1, 1], excel. Cells [1, 1]). Font. Size = 22;

XSt. get_Range (excel. Cells [3, 1], excel. Cells [3, 1]). Font. Bold = true;

// Set the report table to the optimum width
//
XSt. get_Range (excel. Cells [6, 2], excel. Cells [rowIndex, colIndex]). Select ();
XSt. get_Range (excel. Cells [6, 2], excel. Cells [rowIndex, colIndex]). Columns. AutoFit ();

// Set the title of the entire report to center across Columns
//
XSt. get_Range (excel. Cells [1, 1], excel. Cells [1, colIndex]). Select ();
XSt. get_Range (excel. Cells [1, 1], excel. Cells [1, colIndex]). HorizontalAlignment = XlHAlign. xlhaligncenter1_ssselection;
XSt. get_Range (excel. Cells [2, 1], excel. Cells [2, colIndex]). Select ();
XSt. get_Range (excel. Cells [2, 1], excel. Cells [2, colIndex]). HorizontalAlignment = XlHAlign. xlhaligncenter1_ssselection;
XSt. get_Range (excel. Cells [3, 1], excel. Cells [3, colIndex]). Select ();
XSt. get_Range (excel. Cells [3, 1], excel. Cells [3, colIndex]). HorizontalAlignment = XlHAlign. xlhaligncenter1_ssselection;
XSt. get_Range (excel. Cells [4, 1], excel. Cells [4, colIndex]). Select ();
XSt. get_Range (excel. Cells [4, 1], excel. Cells [4, colIndex]). HorizontalAlignment = XlHAlign. xlhaligncenter1_ssselection;
XSt. get_Range (excel. Cells [5, 1], excel. Cells [5, colIndex]). Select ();
XSt. get_Range (excel. Cells [5, 1], excel. Cells [5, colIndex]). HorizontalAlignment = XlHAlign. xlhaligncenter1_ssselection;
//
// Draw a border
//
XSt. get_Range (excel. Cells [6, 1], excel. Cells [rowIndex, colIndex]). Borders. LineStyle = 1;
XSt. get_Range (excel. cells [6, 1], excel. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeLeft]. weight = Microsoft. office. interop. excel. xlBorderWeight. xlThick; // set the left line to bold
XSt. get_Range (excel. cells [6, 1], excel. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeTop]. weight = Microsoft. office. interop. excel. xlBorderWeight. xlThick; // set the upper line to bold
XSt. get_Range (excel. cells [6, 1], excel. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeRight]. weight = Microsoft. office. interop. excel. xlBorderWeight. xlThick; // you can specify the width of the right line.
XSt. get_Range (excel. cells [6, 1], excel. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeBottom]. weight = Microsoft. office. interop. excel. xlBorderWeight. xlThick; // you can specify the width of the lower line.

Excel. Visible = true;

String file = "saved path ";
XBk. SaveCopyAs (file );

 

 

The following is my copywriting code:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. text; 7 using System. windows. forms; 8 9 using MSExcel = Microsoft. office. interop. excel; 10 11 namespace testoffice12 {13 public partial class Form1: Form14 {15 public Form1 () 16 {17 InitializeComponent (); 18} 19 20 private void button#click (object sender, eventArgs e) 21 {22 MSExcel. _ Application excel = new MSExcel. applicationClass (); 23 24 int rowIndex = 6, colIndex = 0, myCCount = 10, myRCount = 3; 25 26 MSExcel. _ Workbook xBk = null; 27 MSExcel. _ Worksheet xSt = null; 28 29 xBk = excel. workbooks. add (true); 30 xSt = (MSExcel. _ Worksheet) xBk. activeSheet; 31 32 // 33 for (int I = 0; I <myCCount; I ++) 34 {35 colIndex ++; 36 xSt. cells [rowIndex, colIndex] = "title" + colIndex. toString (); 37} 38 39 // 40 for (int I = 0; I <myRCount; I ++) 41 {42 rowIndex ++; 43 colIndex = 0; 44 for (int j = 0; j <myCCount; j ++) 45 {46 colIndex ++; 47 xSt. cells [rowIndex, colIndex] = "content" + rowIndex. toString () + ":" + colIndex. toString (); 48 xSt. get_Range (xSt. cells [rowIndex, colIndex], xSt. cells [rowIndex, colIndex]). horizontalAlignment = MSExcel. xlHAlign. xlHAlignCenter; 49} 50} 51 52 xSt. cells [1, 1] = "universe unlimited company"; 53 xSt. cells [2, 1] = "Address"; 54 xSt. cells [3, 1] = "phone fax"; 55 xSt. cells [4, 1] = "customer statement"; 56 xSt. cells [5, 1] = "Operation Date:" + DateTime. now. toLongTimeString (); 57 58 // 59 xSt. get_Range (xSt. cells [1, 1], xSt. cells [1, 1]). font. bold = true; 60 xSt. get_Range (xSt. cells [1, 1], xSt. cells [1, 1]). font. size = 22; 61 xSt. get_Range (xSt. cells [3, 1], xSt. cells [3, 1]). font. bold = true; 62 63 xSt. get_Range (xSt. cells [6, 2], xSt. cells [1, 1]). font. bold = true; 64 65 // 66 for (int I = 1; I <6; I ++) 67 {68 xSt. get_Range (xSt. cells [I, 1], xSt. cells [I, colIndex]). select (); 69 xSt. get_Range (xSt. cells [I, 1], xSt. cells [I, colIndex]). horizontalAlignment = MSExcel. xlHAlign. xlhaligncenter1_ssselection; 70} 71 72 // 73 xSt. get_Range (xSt. cells [6, 1], xSt. cells [rowIndex, colIndex]). borders. lineStyle = 1; 74 xSt. get_Range (xSt. cells [6, 1], xSt. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeLeft]. weight = MSExcel. xlBorderWeight. xlThick; 75 xSt. get_Range (xSt. cells [6, 1], xSt. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeTop]. weight = MSExcel. xlBorderWeight. xlThick; 76 xSt. get_Range (xSt. cells [6, 1], xSt. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeRight]. weight = MSExcel. xlBorderWeight. xlThick; 77 xSt. get_Range (xSt. cells [6, 1], xSt. cells [rowIndex, colIndex]). borders [Microsoft. office. interop. excel. xlBordersIndex. xlEdgeBottom]. weight = MSExcel. xlBorderWeight. xlThick; 78 79 excel. visible = true; 80 string file = "e:/testexcel.xlsx"; 81 xBk. saveCopyAs (file); 82} 83} 84}

 

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.