C # call a class of Excel

xiaoxiao2021-03-06  36

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 /// PUBLIC CLASS OUTPUTEXCEL {Public OutputExcel (DataView DV, String Str) {// // Todo: Add Construction Function Logic // Excel.Application Excel; Int rowindex = 4; int colorex = 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.columns) {Colindex ; Excel.cells [4, ColIndex] = columnname; xst.get_range (Excel.cells [4, ColIndex], Excel. Cells [4, ColIndex]). Horizontalalignment = Excel.xlvalign.xlvalignCenter; // Set the title format to be aligned}

// // Number of data in the table {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; // Setting the date-type field format to center alignment} 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;. // format field character is centered} else {excel.Cells [rowIndex, colIndex] = row [col.ColumnName ] .Tostring ();}}} // // load a total of a total line // 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 part 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, a total of 56 types // // acquired the title // Excel.cells [2, 2, 2] = Str; // / / Setting 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], 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]). Column.autofit (); // // Setting the title of the entire report to cross-ranking // 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.xlhalignCenterCrossSselection; // // 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.xledge ".Weight = Excel.xlborderweight.xlt; // Set the left line plus thick xst.get_range (Excel.cells [4, 2], Excel.cells [4, ColIndex]). Borders [Excel.xlbordersindex.xledgetop] .Weight = Excel.xlborderweight.xlt; // Set the upper edge plus thick xst.get_range (Excel.cells [4, ColIndex], Excel.cells [Rowsum, ColIndex]). Borders [Excel. Xlbordersindex.xledgeright] .Weight = Excel.xlborderweight.xlt; // Set the right line plus thick xst.get_range (Excel.cells [Rowsum, 2], EXC El.cells [Rowsum, Colindex]). Borders [Excel.xlbordersindex.xledgebottom] .weight = Excel.xlborderweight.xlt = Excel.xlborderweight.xlt; // Set the lower edge plus 加 // // 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-71728.html

New Post(0)