原創|其它|編輯:郝浩|2012-10-16 15:02:50.000|閱讀 1004 次
概述:詳細解答了如何在Asp.Net中應用Aspose.Cells輸出報表到Excel 。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本文詳細解答了如何在Asp.Net中應用Aspose.Cells輸出報表到Excel 。
解決思路:
1、安裝Aspose.Cells,下載地址://fc6vip.cn/zh-CN/product/563/download.aspx;
2、在.Net方案中引用此Cells;
3、寫個函數ToExcel(傳遞一個DataTable),可以另寫個SetCellStyle的函數專門給各列設置樣式。
4、在按鈕的Click事件中調用此ToExcel()即可。
部分代碼貼在這里,代碼還有需要改進的地方,暫時這么用著,給自己以后方便查找,再者給真正接觸此控件的同志們拋個磚頭:
ExportToExcel()的:
/// <summary> /// 利用Aspose.Cells對DataTable數據生成Excel文件 /// </summary> /// <returns>是否成功</returns> /// <param name="response">包含存儲路徑,直接用Response即可</param> /// <param name="dt">數據體</param> /// <param name="FileName">文件名</param> /// <param name="SheetName">sheet名</param> /// <param name="Title">表頭</param> /// <param name="ColTitle">列標題,字符串數組</param> /// <param name="ColName">列名,字符串數組</param> /// <param name="ColWidth">列寬,整數數組</param> /// <param name="ColStyle">列樣式,整數數組,1=居中文本、2=特殊字體Georgia、3=居左文本、4=整數(x)、5=2位小數(x,xxx.xx)、6=日期(yyyy-M-d不帶時間)、7=百分數(0.123=12.3%)</param> /// <param name="ColTitleRow">列標題的開始行號,有title的設為1即可</param> /// <param name="err">返回的錯誤信息</param> public bool ExportToExcel(System.Web.HttpResponse response,DataTable dt, string FileName, string SheetName, string Title, ArrayList ColTitle, ArrayList ColName, ArrayList ColWidth, ArrayList ColStyle, int ColTitleRow, ref string err) { //先檢查各數組是否個數一致 if (ColTitle.Count != ColName.Count || ColTitle.Count != ColWidth.Count || ColTitle.Count != ColStyle.Count) { err = "數據組個數不一致"; return false; } try { Workbook workbook = new Workbook(); //打開模版文件 // string path = System.Web.HttpContext.Current.Server.MapPath("~"); // path = path.Substring(0, path.LastIndexOf("//")); // path += @"/designer/Workbooks/NumberFormatting.xls"; // workbook.Open(path); //打開sheet workbook.Worksheets.Clear(); Worksheet worksheet = workbook.Worksheets.Add(SheetName); worksheet = workbook.Worksheets[0]; Cells cells = worksheet.Cells; //cells.ClearContents(0,0,60,30); //加入樣式 ArrayList styles = new ArrayList(); styles = SetCellStyle(workbook, ColStyle); //加入Title即表頭,類似“xxx匯總表”,其占居所有列數 Range w;//范圍 if(ColTitleRow>0) { //Put數據到某個cell中 cells[0,0].PutValue(Title); //設置行高 cells.SetRowHeight(0, 35); //合并單元格 cells.Merge(0,0,1,(byte)ColName.Count); //CreateRange函數參數說明:cells.CreateRange(firstrow, firstcol, rownumber, colnumber) w=cells.CreateRange(0,0,1,(byte)ColName.Count); //設置該合并單元的Style //w.Style = (Aspose.Cells.Style)styles[ColStyle.Count]; w.Style = workbook.Styles["SheetTitle"]; } //給各列的標題行PutValue,類似“序號,類型,名稱,價格,數量,合計” int currow = ColTitleRow; byte curcol = 0; foreach(string s in ColTitle) { cells[currow,curcol++].PutValue(s); cells.SetRowHeight(ColTitleRow, 25); } //設置列標題行的Style w=cells.CreateRange(currow,0,1,ColName.Count); w.Style = (Aspose.Cells.Style)styles[ColStyle.Count+1]; //上面這行也可以寫成 //w.Style = workbook.Styles["ColTitle"];//ColTitle在函數SetCellStyle中設置了 currow++; //將數據體按順序插入各cell for(int i=0; i<dt.Rows.Count; i++) { curcol = 0; for(int j=0; j<ColName.Count; j++) { object val = dt.Rows[i][ColName[j].ToString()].ToString().Trim(); switch (int.Parse(ColStyle[j].ToString())) { case 4://整數 if (val.ToString().Length>0) val = Int32.Parse(val.ToString()); else val = ""; break; case 5://2位小數 if (val.ToString().Length>0) val = Decimal.Parse(val.ToString()); else val = ""; break; case 6://日期 if (val.ToString().Length>0) val = DateTime.Parse(val.ToString()); else val = ""; break; case 7://百分數,1=100% if (val.ToString().Length>0) val = Decimal.Parse(val.ToString()); else val = ""; break; default: break; }//end switch cells[currow,curcol++].PutValue(val); }//end for j currow ++; } //end for i curcol = 0; //設置數據體Style for(int i=0; i<dt.Columns.Count; i++) { w = cells.CreateRange(ColTitleRow+1, i, dt.Rows.Count, 1); w.Style = (Aspose.Cells.Style)styles[i]; } //w=cells.CreateRange(ColTitleRow+1,col,currow,ColName.Count); //w.Style=excel.Styles["Data"]; //設置各列寬度 foreach(int s in ColWidth) cells.SetColumnWidth(curcol++, s); /* //********可參考的格式設置******************************************************* //Set number format with built-in index for (int i = 0; i < 37; i ++) { cells[i, 1].PutValue(1234.5); //int Number = cells[i, 0].IntValue; //Set the display number format cells[i, 1].Style.Number = i; } //Set number format with custom format string for (int i = 1; i < 4; i ++) { cells[i, 3].PutValue(1234.5); //Set the display custom number format cells[i, 3].Style.Custom = cells[i, 2].StringValue; } //********可參考的格式設置******************************************************* */ //workbook.Save(FileName, FileFormatType.Default, SaveType.OpenInExcel, System.Web.HttpContext.Current.Response); workbook.Save(FileName, FileFormatType.Default, SaveType.OpenInExcel, response); return true; } catch (Exception ex) { err = ex.Message; return false; } }//end ExportToExcel() #endregion
SetStyle()的:
#region SetCellStyle()設置格式,如果需要增加新的格式,請在case后面增加,不要修改前面的
public ArrayList SetCellStyle(Workbook workbook, ArrayList styleindex) { //通用設置樣式的 ArrayList CellStyle = new ArrayList(styleindex.Count + 2); Aspose.Cells.Style style = null; for (int i=0; i<styleindex.Count; i++) { int index = workbook.Styles.Add(); style = workbook.Styles[index]; style.Name = "Custom_Style" + ((int)(i + 1)).ToString(); style.ForegroundColor = Color.White; style.HorizontalAlignment = TextAlignmentType.Center; style.VerticalAlignment = TextAlignmentType.Center; style.Font.Name = "宋體"; style.Font.Size = 10; style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; switch((int)styleindex[i]) { case 1://居中文本 style.HorizontalAlignment = TextAlignmentType.Center; style.VerticalAlignment = TextAlignmentType.Center; break; case 2://特殊字體Georgia style.Font.Name = "Georgia"; break; case 3://文本列 style.HorizontalAlignment = TextAlignmentType.Left; break; case 4://整數列 1 Decimal 0 style.HorizontalAlignment = TextAlignmentType.Center; style.Number = 1; break; case 5://2位小數 39={Currency #,##0.00;-#,##0.00} 40={#,##0.00;[Red]-#,##0.00} style.HorizontalAlignment = TextAlignmentType.Right; style.Number = 40; break; case 6://日期列 14 Date yyyy-m-d style.HorizontalAlignment = TextAlignmentType.Center; style.Number = 14;//這個格式不是太好,還需要調整 break; case 7://百分比% 10 Percentage 0.00% style.HorizontalAlignment = TextAlignmentType.Center; style.Number = 10; break; default: break; }//end switch CellStyle.Add(style); } //特別增加一個用于表頭的style.Name = "SheetTitle"; int sindex = workbook.Styles.Add(); style = workbook.Styles[sindex]; style.Name = "SheetTitle"; style.Font.Size = 14; style.Font.IsBold = true; style.Font.Name = "楷體_GB2312"; style.HorizontalAlignment = TextAlignmentType.Center; style.VerticalAlignment = TextAlignmentType.Center; style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; CellStyle.Add(style); //特別增加一個用于列標題的style.Name = "ColTitle"; sindex = workbook.Styles.Add(); style = workbook.Styles[sindex]; style.Name = "ColTitle"; style.Font.Size = 12; style.Font.IsBold = true; style.Font.Name = "宋體"; style.HorizontalAlignment = TextAlignmentType.Center; style.VerticalAlignment = TextAlignmentType.Center; style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; CellStyle.Add(style); //返回樣式數組 return CellStyle; }//end SetCellStyle #endregion
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:Wsq2002的專欄-CSDN