C # call a class of Excel

zhaozj2021-02-12  139

9CBS - Document Center - .NET Title C # Call Operation Excel A Cocosoft (Original) Keyword Excel C # call

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 running: In this class, there are two parameters passed, 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 // Excel.Application Excel; int = 4; int storageex = 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] = columnname; xst.get_range (Excel.cells [4, Colindex], Excel.cells [4, Colindex]). Horizontalalignment = Excel.xlvalign.xlvalignCenter; // Set the title format to be aligned in the center} // // Data in the table {RowIndex ; ColIndex ; colorh (Datacolumn col in dv.table.columns) {Colindex ; if (col.DataType == System.Type.gettype ("System.datetime")) {excel.cells [rowindex, colIndex] = (convert.t) odatetime (row [columnnname] .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 is aligned} 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 characters Type field format is aligned in the center} else {excel.cells [RowIndex, ColIndex] =

Row [color ();}}} // // load a total of a total bank // 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 Color // Xst.get_Range (Excel.cells [ Rowsum, colsum], excel.cells [rowsum, colorex]). 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] = Str; // // 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 is the cross-column center // xst.get_range (Excel.cells [2, 2, 2], Excel. Cells [2, Colindex]). SELECT (); xst.get_range (Excel.cel) LS [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. XLTHICK; // Set the left cable plus thick xst.get_range (Excel.cells [4, 2], Excel.cells [4, Colindex]). Borders [Excel.xlbordersindex.xledgeTGetop] .weight = Excel.xlborderweight.xlt; / Set the upper side line plus xst.get_range (Excel.cells [4, ColIndex], Excel.cells [Rowsum, ColIndex]). Borders [Excel.xlbordersindex.xledgeright] .Weight = Excel.xlborderweight.xlt;

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

New Post(0)