C # call a class of Excel
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'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 the 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; // Set the title format to be aligned}
//// Number of 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; // Setting the date type field format is aligned} Elseif (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 row // int ROWSUM = ROWINDEX 1; int COLSUM = 2; Excel.cells [Rowsum, 2] = "Total"; xst.get_range (Excel.cells [ROWSUM, 2], Excel.cells [Rowsum, 2]). HorizontalaLAlignment = Excel.xlhalign.xlhalignCenter; //// Set the color of the selected portion // x St.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 species //// acquired the 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] ).bold = true; xst.get_range (Excel.cells [2, 2, 2], Excel.cells [2, 2 ]). Font.size = 22; //// Settings the report form for the most adaptive width //xst.get_range (Excel.cells[RowSum, Coldex] ).select();xst .get_range (Excel.cells [4, 2], Excel.cells [Rowsum, ColIndex]). Columns.autofit ();