string file = Filedialoghelper.saveexcel (string. Format ("{0}.xls", excelname)); if (!string. IsNullOrEmpty (file) && dt. Rows.Count > 0) {try {string error = ""; Datatabletoexcel (dt, file, out error); if (!string. IsNullOrEmpty (Error)) {Messagedxutil.showerror (string. Format ("Export Excel error: {0}", error)); } else {if (Messagedxutil.showyesnoandtips ("Export succeeded, do you want to open the file? ") = = System.Windows.Forms.DialogResult.Yes) {System.Diagnostics.Proces S.start (file); }}} catch (Exception ex) {Logtexthel Per. Error (ex); Messagedxutil.showerror (ex. Message); } }
Action on Aspose
Error = ""; Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook (); try {if (datatable = = null) {error = "Datatabletoexcel:datat Able is empty "; return false; }//Add style to cell Aspose.Cells.Style style = wb. STYLES[WB. Styles.add ()]; Sets the center style. HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; Sets the background color style. Foregroundcolor = System.Drawing.Color.FromArgb (153, 204, 0); Style. Pattern = Backgroundtype.solid; Style. Font.isbold = true; Add a style to a cell Aspose.Cells.Style Styleheader = wb. STYLES[WB. Styles.add ()]; Set Center styleheader.horizontalalignment = Aspose.Cells.TextAlignmentType.Center; Set background color styleheader.foregroundcolor = System.Drawing.Color.Gold; Styleheader.borders[aspose.cells.bordertype.leftborder]. LineStyle = Aspose.Cells.CellBorderType.Medium; Application boundary line left boundary Styleheader.borders[aspose.cells.bordertype.rightborder]. LineStyle = Aspose.Cells.CellBorderType.Medium; Apply boundary line right boundary Styleheader.borders[aspose.cells.bordertype.topborder]. LineStyle = Aspose.Cells.CellBorderType.Medium; Apply boundary line on boundary line Styleheader.borders[aspose.cells.bordertype.bottomborder]. LineStyle = Aspose.Cells.CellBorderType.Medium; The boundary line of application boundary line Styleheader.pattern = Aspose.Cells.BackgroundType.Solid; StyleHeader.Font.IsBold = true; int rowIndex = 0; Aspose.Cells.Range Range; Aspose.Cells.Cell Cell; Range = wb. Worksheets[0]. Cells.createrange (0, 0, 2, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Evaluation Project"); //Range. SetStyle (Styleheader); Range = wb. Worksheets[0]. Cells.createrange (0, 1, 2, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Evaluation score"); Range = wb. Worksheets[0]. Cells.createrange (0, 2, 1, 3); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Score results (not suitable for project adjustments)"); Range = wb. Worksheets[0]. Cells.createrange (1, 2, 1, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Soundness of System"); Range = wb. Worksheets[0]. Cells.createrange (1, 3, 1, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Follow the validity"); Range = wb. Worksheets[0]. CelLs. Createrange (1, 4, 1, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Total"); Range = wb. Worksheets[0]. Cells.createrange (0, 5, 2, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Score results (not suitable for project adjustment)"); Range = wb. Worksheets[0]. Cells.createrange (0, 6, 2, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("Weight"); Range = wb. Worksheets[0]. Cells.createrange (0, 7, 2, 1); Range. Merge (); Range. SetStyle (Styleheader); Cell = range[0, 0]; Cell. Putvalue ("final result"); Aspose.Cells.Range Range1 = wb. Worksheets[0]. Cells.createrange (0, 1, 2, 1); Range1. Merge (); Aspose.Cells.Range range2 = wb. Worksheets[0]. Cells.createrange (0, 2, 1, 3); Range2. Merge (); Aspose.Cells.Range range3 = wb. Worksheets[0]. Cells.createrange (0, 5, 2, 1); Range3. Merge (); Aspose.Cells.Range range4 = wb. Worksheets[0]. Cells.createrange (0, 6, 2, 1); Range4. Merge (); Aspose.Cells.Range range5 = wb. Worksheets[0]. Cells.createrange (0, 7, 2, 1); Range5. Merge (); Wb. Worksheets[0]. Cells[rowindex, 0]. Putvalue ("Evaluation Project"); Wb. Worksheets[0]. Cells[rowindex, 0]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[rowindex, 1]. Putvalue ("Evaluation score"); Wb. Worksheets[0]. Cells[rowindex, 1]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[rowindex, 2]. Putvalue ("Score results (not suitable for project adjustments)"); Wb. Worksheets[0]. Cells[rowindex, 2]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[1, 2]. Putvalue ("Soundness of System"); Wb. Worksheets[0]. Cells[1, 2]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[1, 3]. Putvalue ("Follow the validity"); Wb. Worksheets[0]. Cells[1, 3]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[1, 4]. Putvalue ("Total"); Wb. Worksheets[0]. Cells[1, 4]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[rowindex, 5]. Putvalue ("Score results (not suitable for project adjustment)"); Wb. Worksheets[0]. Cells[rowindex, 5]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[rowindex, 6]. Putvalue ("Weight"); Wb. Worksheets[0]. Cells[rowindex, 6]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[rowindex, 7]. Putvalue ("final result"); Wb. Worksheets[0]. Cells[rowindex, 7]. SetStyle (Styleheader); Styleheader.foregroundcolor = System.Drawing.Color.LightCoral; Wb. Worksheets[0]. Cells[2, 0]. Putvalue ("(1)"); Wb. Worksheets[0]. Cells[2, 0]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[2, 1]. Putvalue ("(2)"); Wb. Worksheets[0]. Cells[2, 1]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[2, 2]. Putvalue ("(3)"); Wb. Worksheets[0]. Cells[2, 2]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[2, 3]. Putvalue ("(4)"); Wb. Worksheets[0]. Cells[2, 3]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[2, 4]. Putvalue ("(5) = (3) + (4)"); Wb. Worksheets[0]. Cells[2, 4]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[2, 5]. Putvalue ("(6)"); Wb. Worksheets[0]. Cells[2, 5]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[2, 6]. Putvalue ("(7)"); Wb. Worksheets[0]. Cells[2, 6]. SetStyle (Styleheader); Wb. Worksheets[0]. Cells[2, 7]. Putvalue ("(8) = (6) x (7)"); Wb. Worksheets[0]. Cells[2, 7]. SetStyle (stylEheader); for (int i = 0; i < DataTable. Columns.count; i++) {DataColumn col = DataTable. Columns[i]; String columnName = Col. Caption?? Col. ColumnName; Wb. Worksheets[0]. Cells[rowindex, I]. Putvalue (ColumnName); Wb. Worksheets[0]. Cells[rowindex, I]. SetStyle (style); } rowindex++; RowIndex = 3; foreach (DataRow row in DataTable. Rows) {for (int i = 0; i < DataTable. Columns.count; i++) {wb. Worksheets[0]. Cells[rowindex, I]. Putvalue (Row[i]. ToString ()); Wb. Worksheets[0]. Cells[rowindex, I]. SetStyle (style); } rowindex++; } string excelheader = "Projectname,standardscore,sysintegrity,followeffectiveness,total,scoreresult,weight , Endresult "; ForeaCH (DataRow row in DataTable. Rows)//{//int i = 0; foreach (String col in Excelheader.split (', '))///{//WB. Worksheets[0]. Cells[rowindex, I]. Putvalue (Row[col]. ToString ()); Wb. Worksheets[0]. Cells[rowindex, I]. SetStyle (style); i++; }//rowindex++; }//for (int k = 0; k < DataTable. Columns.count; k++)//{//WB. Worksheets[0]. Autofitcolumn (k, 0, 150); } WB. Worksheets[0]. FreezePanes (1, 0, 1, DataTable. Columns.count); Wb. Save (filepath); return true; } catch (Exception e) {error = error + "Datatabletoexcel:" + e.message; return false; }
Aspose Exporting Excel Simple operations