Summarize C# Control Excel Skills
0. Import Namespace
using Microsoft.Office.Core; using Microsoft.Office.Interop.Excel; using System.IO; using System.Reflection;
1. How to open an Excel file, or create a new Exceil file
Application app = new Application(); Workbooks wbks = app.Workbooks; _Workbook _wbk = wbks.Add(xxx); Open an exist Excel file, please replace “xxx” with the excel file path. Create a new Excel file, please replace “xxx” with “true”. Note: there is only one worksheet in the excel file.
2. Get, delete and add sheet
Sheets shs = _wbk.Sheets;
2.1 Get
//i is index of the sheet which you want get: _Worksheet _wsh = (_Worksheet)shs.get_Item(i)
2.2 Delete
//must do when delete app.DisplayAlerts = false; _wsh.Delete();
2.3 Add
//a(before),b(after):Besure position;c:amount;d:type app.Worksheets.Add(a,b,c,d);
2.4 rename sheet
_wsh.Name = "xxx";
3. Delete rows and columns
3.1 Delete rows
((Range)_wsh.Rows[3, Missing.Value]).Delete(XlDeleteShiftDirection.xlShiftUp);
3.2 Delete columns
_wsh.get_Range( _wsh.Cells[1, 2], _wsh.Cells[_wsh.Rows.Count, 2]).Delete(XlDeleteShiftDirection.xlShiftToLeft );
4. Add rows and columns
4.1 Add rows
((Range)_wsh.Rows[11, Missing.Value])
.Insert(Missing.Value, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
4.2 Add columns
_wsh.get_Range( _wsh.Cells[1, 1], _wsh.Cells[_wsh.Rows.Count, 1]) .Insert(Missing.Value, XlInsertShiftDirection.xlShiftToRight);
5. Format cells
5.1 Get cell
//Get cell object _wsh.Cells[row, cell]
5.2 Set formula
//input formula in the cell _wsh.Cells[row, cell] = "=Sum(A1/B1)";
5.3 Merge cells
((Range)_wsh.Rows[1, Missing.Value]).Merge(Missing.Value);
5.4 Set row height and column width
((Range)_wsh.Rows[3, Missing.Value]).RowHeight = 5; ((Range)_wsh.Rows[3, Missing.Value]).ColumnWidth = 5;
5.5 Set cell color (56 choices) color table attached
((Range)_wsh.Rows[1, Missing.Value]).Interior.ColorIndex = 3;
5.6 Set font size
((Range)_wsh.Cells[1, "B"]).Font.Size = 8;
5.7 Set font bold
((Range)_wsh.Rows[1, Missing.Value]).Font.Bold = false;
5.8 Set Cell or field horizontal and center
((Range)_wsh.Cells[2, 1]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
5.9 Set Field borders
((Range)_wsh.Cells[3, 3]).Borders.LineStyle = 3;
5.10 Set border lines (upside, downside, left side and right side)
//Left side boder line _wsh.get_Range( _wsh.Cells[2, 1], _wsh.Cells[2, 2]) .Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;// //Right side border line _wsh.get_Range( _wsh.Cells[2, 1], _wsh.Cells[2, 2]) .Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;// //Upside border line _wsh.get_Range( _wsh.Cells[2, 1], _wsh.Cells[2, 2]) .Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//下 //Downside border line _wsh.get_Range( _wsh.Cells[2, 1], _wsh.Cells[2, 2]) .Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;
6. Copy Selected Field
_Worksheet _wsh = (_Worksheet)shs.get_Item(1);// Copy selected field content Range range = _wsh.get_Range(_wsh.Cells[7, 1], _wsh.Cells[10, _wsh.Columns.Count]); range.Select(); range.Copy(Type.Missing); //Select paste starting position Range test = ((Range)_wsh.Cells[11, 1]); test.Select(); // Shield Alert, default confirm paste app.DisplayAlerts = false; test.Parse(Missing.Value, Missing.Value);
Note: Type Missing and Missing Value are considered as some parameters' default value. Most of time, they are form complemented parameters.
7. Export data from database to Excel
Click here to know How to export Database to Excel
Codes from Free Data Export Component
8. Save excel and follow-up processes
8.1 Save Excel file
// Shield Alert popped up from sysytem app.AlertBeforeOverwriting = false; // Save to selected file path SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Note: It’s only solution for saving the excel file. Otherwise, there will generate a corresponding copy in “My File”
8.2 Follow-up processes: Exit and release
//_wbk.Close(null, null, null); //wbks.Close(); app.Quit(); // Release unnecessary excel processes System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null;
Note: During the process of closing application, we usually have 2 solutions
- directly exit app
- Close workbook, close workbooks, exit app. (Recommend)
9. Set Cell field and get the necessary data from field
9.1 If cell has been set as drop-down box
// Here the “1, 2, 3” means the drop-down box value ((Range)_wsh.Cells[2, 1]) .Validation.Modify(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, Type.Missing, "1,2,3", Type.Missing);
9.2 If cell has not been set as drop-down box
((Range)_wsh.Cells[2, 1]) .Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, Type.Missing,"1,2,3", Type.Missing);
9.3 Get drop-down box field value
string strValue = ((Range)_wsh.Cells[2, 1]).Validation.Formula1;
Note: In the excel template, if set drop-down box value through effectiveness, strValue will get the formula of excel, which need be converted. After get strValue, you can get the numerical value according to index.
10. Hide rows and hide columns
10.1 Hide rows
_wsh.get_Range(_wsh.Cells[19, 1], _wsh.Cells[22, 1]).EntireRow.Hidden = true;
10.2 Hide columns
_wsh.get_Range(_wsh.Cells[1, 1], _wsh.Cells[_wsh.Rows.Count, 1]) .EntireColumn.Hidden = true;
发表评论
HYh4rs This is one awesome article post.Much thanks again. Much obliged.
YcfL8V You may have a very good layout for the blog i want it to utilize on my web site as well
hvL9tg Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is magnificent, as well as the content!
3jHnlk You can definitely see your expertise within the work you write.
bK35Fx Online Article Every once in a while we choose blogs that we read. Listed underneath are the latest sites that we choose
AM7u4h Way cool! Some very valid points! I appreciate you writing this article and the rest of the website is very good.
EFnLEP Hello! I've been following your website for a while now and finally got the courage to go ahead and give you a shout out from Houston Tx! Just wanted to say keep up the great job!
I think this is a real great article post. Keep writing.
Enjoyed every bit of your blog post.Much thanks again. Fantastic.
A round of applause for your article post. Great.
I am so grateful for your post.Really thank you! Cool.
Im obliged for the blog article.Much thanks again. Much obliged.
I truly appreciate this article post.Thanks Again. Great.
Great, thanks for sharing this article.Really looking forward to read more. Fantastic.
A round of applause for your blog article. Great.
Im thankful for the blog post. Keep writing.
I truly appreciate this post. Great.
I value the post.Really looking forward to read more.
Appreciate you sharing, great blog.Really looking forward to read more. Much obliged.
Thank you for your blog article.Really thank you! Will read on...
Major thankies for the post. Fantastic.
Really enjoyed this blog. Fantastic.
Really informative blog post.Much thanks again. Awesome.
Awesome blog article. Cool.
Very good post.Much thanks again. Great.
I cannot thank you enough for the blog post.Much thanks again. Great.
Very informative article.Really looking forward to read more. Great.
Thanks-a-mundo for the blog.Really thank you! Will read on...
Enjoyed every bit of your post. Great.
I really like and appreciate your blog.Thanks Again. Much obliged.
Thank you for your article.
Thank you ever so for you blog article.Much thanks again. Awesome.
Looking forward to reading more. Great post.Much thanks again. Cool.
Thanks so much for the post.Much thanks again. Cool.
Really enjoyed this article post.Really thank you! Great.
Great article post.Much thanks again. Really Great.
Muchos Gracias for your blog.Really looking forward to read more. Keep writing.
I really liked your post.Thanks Again. Awesome.
Appreciate you sharing, great article post.Much thanks again. Really Cool.
Thank you for your blog.Really looking forward to read more. Want more.
Thanks again for the post.Thanks Again. Keep writing.
Great, thanks for sharing this blog article.Much thanks again. Awesome.
Awesome blog article. Will read on...
I really liked your article.Much thanks again. Great.
Enjoyed every bit of your article post.Really looking forward to read more. Keep writing.
Thanks-a-mundo for the blog.Really looking forward to read more. Cool.
I think this is a real great blog article.Much thanks again.
Thanks for the article post. Great.
Really appreciate you sharing this post. Will read on...
Very informative post.Much thanks again. Fantastic.