Use a template to export an excel table

Source: Internet
Author: User
I read some articles about using c # To export excel on the Internet, and I felt very full. However, it may be because all of them are confused. I thought about it all morning, I made a very simple one, but I personally feel very practical to use the template to export the excel table program, paste it out and share it with friends who need it.
I am still a student. If you have any ideas, please advise!
Namespace to be introduced:
Using System. Reflection;
Using Microsoft. Office. Interop. Excel; (you need to add the com component Microsoft Excel 11.0 Object Library. Excel is installed! Excel, microsoft. offic. core. office, and VBIDE are added to the reference)
By the way, I would like to introduce a small technique. If a class instance is created, if it is located using a namespace, if it is found that the name is too long, you can define an alias for it, for example: using Excel = Microsoft. office. interop. excel;
So Excel is the alias of Microsoft. Office. Interop. Excel!

# Region Excel variable (the comment below is also defined below for ease of understanding !)
// Excel object references.
// Private Excel. Application m_objExcel = null;
// Private excel. workbooks m_objbooks = NULL;
// Private excel. _ workbook m_objbook = NULL;
// Private excel. Sheets m_objsheets = NULL;
// Private excel. _ worksheet m_objsheet = NULL;
Private excel. Range m_objrange = NULL;
Private object m_objopt = missing. value;
Private object m_strsamplefolder;
# Endregion
// Open an Excel application
Private excel. Application m_objexcel = new excel. Application ();
// Open an existing workbook
Private excel. workbooks m_objbooks = m_objexcel.workbooks;
Private excel. _ workbook m_objbook = NULL;

M_objbook = upper (strpath + @ "\ Excel \ table 1-9 lower", m_objopt, m_objopt, lower, lower, m_objopt, lower, m_objopt, m_objopt, m_objopt );
// Open the sheet page
Private excel. Sheets m_objsheets = NULL;

M_objsheets = (Excel. Sheets) m_objbook.worksheets;
// Select the first sheet
Private excel. _ worksheet m_objsheet = NULL;

M_objsheet = (Excel. _ worksheet) (m_objsheets.get_item (1 ));

// The value can be assigned below.
Simplest
M_objrange = m_objsheet.get_range ("B" + (5 + I), m_objopt); // locate the position to be filled in
M_objRange.set_Value (m_objOpt, Array [0]); // assigned a value (in this example, arry is an Array of the object type, and the definition is also object, however, since everything is inherited from an object, it can be used for numbers, strings, and so on !)

No m_objSheet.get_Range ("B" + (5 + I), m_objOpt) is found. It is get_Range. That is to say, it is a range. If it is get_Range ("B5", "U5 ") that is to fill from B5 to U5, you can fill with a one-dimensional array, if it is B5 to U8 ?? Fill and scatter with a two-dimensional array !! The "B" + (5 + I) is defined by the younger brother to facilitate the use of one-dimensional array loops. The result is similar to B5, I have spoken a lot. No one knows this when I write a program ,!
Note that if your table contains merging items, for example, B5, C5, and D5 are cells, you cannot use arrays to get lazy. You must specify them one by one, (I did not find this method. If you find it, please let me know !)

The final work will be completed:
Values (values, m_objOpt, Excel. XlSaveAsAccessMode. xlNoChange, m_objOpt, m_objOpt );

M_objBook.Close (false, m_objOpt, m_objOpt );
M_objExcel.Quit ();
Description: m_strSampleFolder is the name of the object to be saved.
Write it together:

SaveFileDialog saveDlg = new SaveFileDialog ();
SaveDlg. FileName = @ "Table 1-9 standard geographic Price Assessment Technical highlights table .xls ";

SaveDlg. Filter = "Excel files (*. xls) | *. xls ";
If (saveDlg. ShowDialog () = DialogResult. OK)
{
M_strSampleFolder = saveDlg. FileName;
// Start a new workbook in Excel.
M_objExcel = new Excel. Application ();
M_objBooks = m_objExcel.Workbooks;
M_objBook = upper (strPath + @ "\ excel \ table 1-9 lower", m_objOpt, m_objOpt, lower, lower, m_objOpt, lower, m_objOpt, m_objOpt, m_objOpt );
M_objSheets = (Excel. Sheets) m_objBook.Worksheets;
M_objSheet = (Excel. _ Worksheet) (m_objSheets.get_Item (1 ));
Object [] Array = new object [7]; // from name to quasi-price 7

// Lower
}

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.