C # call a class of Excel

zhaozj2021-02-16  52

I would like to write a little bit of articles here, but because of time relationship, I have not been written. Now, a little thing you have, C # calls Excel's source code to make a report here to give you a look. Regarding the composition of the code, there is already a complete code annotation in the source code, which is nothing here.

In this class, the main function is to read the data from the database, and after setting the format, it is displayed in Excel. This is the rendering after it runs:

In this class, there are two parameters passed in, one is its data source, the other is the title string of the entire report, and the code should be understood.

Using system; using system.data; using Excel;

Namespace logiclayer {? ///

? /// OUTPUTEXCEL 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 the title ??? // ??? foreach (Datacolumn COL in DV.Table.Column) ??? {???? colIndex ; ???? Excel.cells [4 , Colindex] = col.columnname; ???? xst.get_range (Excel.cells [4, Colindex], Excel.cells [4, Colindex]). Horizontalalignment = Excel.xlvalign.xlvalignCenter; // Set the title format Align ???}

??? // ??? // 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.TODATEX) (Row [columnName] .tostring ())). Tostring (" YYYY-MM- DD "); ?????? Xst.get_Range (Excel.cells [RowIndex, Colindex], Excel.cells [RowIndex, Colindex]). Horizontalalignment = Excel.xlvalign.xlvalignCenter; // Setting the date type field format Central alignment ?????} ????? ELSE ????? if (col.DataType == system.type.gettype ("system.string")) ????? {????? EXCEL.CELLS [RowIndex, ColIndex] = "'" row [color (); ?????? xst.get_range (Excel.cells [RowIndex, ColIndex], Excel.cells [RowIndex, COLINDEX]). Horizontalalignment = Excel.xlvalign.xlvalignCenter; // Set the field format of the character pattern to be aligned ?????} ????? Else ????? {?????? Excel.cells [RowIndex, ColIndex] = ROW [col.columnname] .tostring (); ?????} ????} ???} ??? // ??? // load a total of a total row ??? / / ??? Int rowsum = rowindex 1; ??? INT colsum = 2; ??? Excel.cells [ROWS UM, 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; // Set to light yellow, total 56 species ??? // ??? // acquire the entire report Title ??? // ??? Excel.cells [2, 2] = Str; ??? // ??? // set the title format of the entire report ??? // ??? xst.get_range (Excel .Cells [2, 2], Excel.cells [2, 2]). Font.bold = true; ??? xst.get_range (Excel.cells [2, 2, 2, Excel.cells [2, 2]). Font.size = 22;

??? // ??? // Set the report form to the most adaptive 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 as Cross-column in the middle ??? // ??? 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.xlhaligncenteraCrossSselection; ??? // ??? // 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.xlt; // Set the left cable to add thick ??? xst.get_range (Excel.cells [4, 2], Excel.cells [4, Colindex]). Borders [Excel .Xlbordersindex.xledgetop] .Weight = Excel.xlborderweight.xlt; // Set the upper side line plus thick ??? xst.get_range (Excel.cells [Rowsum, ColIndex]). Borders [Excel .Xlbordersindex.xledgeright] .Weight = Excel.xlborderweight.xlt; // Set the right line to add thick ??? xst.get_range (Excel.cells [Rowsum, 2], Excel.cells [Rowsum, ColIndex]). Borders [Excel.xlbordersindex.xledgebottom] .weight = Excel.xlborderweight.xlt = Excel.xlborderweight.xlt; // Set the lower edge plus thick ??? // ??? // display effect ??? / / ??? EXCEL.VISIBLE = True; ??}?}} Someone is asking, how to end the Excel process? It can be forced to end the process :) Generally used garbage automatic recycling technology, but in order to end the Excel process more. We can add an event: Judging whether the Excel is still in the process set. If there is, returns true; of course, if not, return false. When the event is true, it will use the forced process to kill the technology.

转载请注明原文地址:https://www.9cbs.com/read-24066.html

New Post(0)