Excel
Here originally wanted to write a long article, but because of the relationship between time, not written. Now I do a small thing, C # call Excel as a report source code put here to show you. As for the composition of the code, there is already a complete code comment in the source code, and there is nothing to say here.
In the following class, the main function is to read the data verbatim from the database, formatted, and displayed in Excel. This is the effect of the diagram after it runs:
In this class, there are two parameters passed in, one is its data source, the other is the entire report title string, the specific look at the code should know.
Using System;
Using System.Data;
Using Excel;
Namespace Logiclayer
{
?///
?///Outputexcel's summary description
?///
? public class Outputexcel
? {
?? Public Outputexcel (DataView dv,string str)
?? {
??? //
??? TODO: Add constructor logic here
??? //
??? Excel.Application Excel;
??? int rowindex=4;
??? int colindex=1;
??? Excel._workbook XBK;
??? Excel._worksheet xSt;
??? Excel= new Excel.applicationclass ();
??? XBK = Excel. Workbooks.Add (TRUE);
??? XSt = (excel._worksheet) xbk.activesheet;
??? //
??? Get title
??? //
??? foreach (DataColumn col in DV. Table.columns)
??? {
???? colindex++;
???? Excel. Cells[4,colindex] = Col. ColumnName;
???? Xst.get_range (Excel. Cells[4,colindex],excel. Cells[4,colindex]). HorizontalAlignment = excel.xlvalign.xlvaligncenter;//formatting headings in center alignment
???}
??? //
??? Get the data in the table
??? //
??? foreach (DataRowView row in DV)
??? {
???? RowIndex + +;
???? Colindex = 1;
???? foreach (DataColumn col in DV. Table.columns)
???? {
????? Colindex + +;
????? if (Col. DataType = = System.Type.GetType ("System.DateTime"))
????? {
?????? Excel. Cells[rowindex,colindex] = (Convert.todatetime (row[col). ColumnName]. ToString ())). ToString ("Yyyy-mm-dd");
?????? Xst.get_range (Excel. Cells[rowindex,colindex],excel. Cells[rowindex,colindex]). HorizontalAlignment = excel.xlvalign.xlvaligncenter;//The field format of the date type is centered
?????}
????? Else
????? if (Col. DataType = = System.Type.GetType ("System.String"))
????? {
?????? Excel. Cells[rowindex,colindex] = "'" +row[col. ColumnName]. ToString ();
?????? Xst.get_range (Excel. Cells[rowindex,colindex],excel. Cells[rowindex,colindex]). HorizontalAlignment = excel.xlvalign.xlvaligncenter;//The field format of the character type is centered
?????}
????? Else
????? {
?????? Excel. Cells[rowindex,colindex] = Row[col. ColumnName]. ToString ();
?????}
????}
???}
??? //
??? Load an aggregate row
??? //
??? int rowsum = RowIndex + 1;
??? int colsum = 2;
??? Excel. cells[rowsum,2] = "Total";
??? Xst.get_range (Excel. Cells[rowsum,2],excel. cells[rowsum,2]). HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
??? //
??? Set the color of the selected section
??? //
??? Xst.get_range (Excel. Cells[rowsum,colsum],excel. Cells[rowsum,colindex]). Select ();
??? Xst.get_range (Excel. Cells[rowsum,colsum],excel. Cells[rowsum,colindex]). Interior.ColorIndex = 19;//is set to light yellow with a total of 56
??? //
??? Get the title of the entire report
??? //
??? Excel. cells[2,2] = str;
??? //
??? Set the title format for the entire report
??? //
??? Xst.get_range (Excel. Cells[2,2],excel. cells[2,2]). Font.Bold = true;
??? Xst.get_range (Excel. Cells[2,2],excel. cells[2,2]). Font.Size = 22;
??? //
??? Set the report table to fit the width
??? //
??? Xst.get_range (Excel. Cells[4,2],excel. Cells[rowsum,colindex]). Select ();
??? Xst.get_range (Excel. Cells[4,2],excel. Cells[rowsum,colindex]). Columns.AutoFit ();
??? //
??? Set the title of the entire report to center across columns
??? //
??? Xst.get_range (Excel. Cells[2,2],excel. Cells[2,colindex]). Select ();
??? Xst.get_range (Excel. Cells[2,2],excel. Cells[2,colindex]). HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
??? //
??? Draw Border
??? //
??? Xst.get_range (Excel. Cells[4,2],excel. Cells[rowsum,colindex]). Borders.LineStyle = 1;
??? Xst.get_range (Excel. Cells[4,2],excel. cells[rowsum,2]). Borders[excel.xlbordersindex.xledgeleft]. Weight = excel.xlborderweight.xlthick;//Set left line bold
??? Xst.get_range (Excel. Cells[4,2],excel. Cells[4,colindex]). Borders[excel.xlbordersindex.xledgetop]. Weight = excel.xlborderweight.xlthick;//Set top edge Bold
??? Xst.get_range (Excel. Cells[4,colindex],excel. Cells[rowsum,colindex]). Borders[excel.xlbordersindex.xledgeright]. Weight = excel.xlborderweight.xlthick;//Set the right line bold
??? Xst.get_range (Excel. Cells[rowsum,2],excel. Cells[rowsum,colindex]). Borders[excel.xlbordersindex.xledgebottom]. Weight = excel.xlborderweight.xlthick;//Set Bottom edge Bold
??? //
??? Display effects
??? //
??? Excel. Visible=true;
??}
?}
}
Someone is asking, how do you end an Excel process?
Can force end process:)
Garbage automatic recycling technology is generally used, but in order to end the Excel process more thoroughly. We can add an additional event: To determine if Excel is still in the process set. Returns True if there is, of course, false if not.
When the event is true, the use of a mandatory process to kill the technology will do.
If you want to determine whether Excel is in the process set, refer to the following code:
??????????? int proceedingcount = 0;
??????????? process[] Proceddingcon = process.getprocesses ();
??????????? foreach (Process isprocedding in Proceddingcon)
??????????? {
??????????????? if (Isprocedding.processname = = "Excel")
??????????????? {
??????????????????? Proceedingcount + 1;
??????????????? }
??????????? }
??????????? if (Proceedingcount > 0)
??????????? {
??????????????? MessageBox.Show ("The system is already running Excel.) "," hint ", messageboxbuttons.ok,messageboxicon.information);
return true;
??????????? }
Else
{
return false;
}
??}
==================
To kill the process, use an event called Kill (). It seems to be so. :)