您好,登錄后才能下訂單哦!
使用Aspose.Cells怎么實現萬能導出功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
導出Excel無非就是取出數據,然后利用Aspose.Cells插件填充到Excel文件中,DataTable類型的數據是最適合填充Excel不過了.唯一的問題就是DataTable數據的列頭一般是英文,突然就想出了利用SQL Server每一列的說明來替換掉英文列頭的方法,我太TM機智了.
/// <summary> /// 導出 /// </summary> /// <param name="dt">導出的數據表</param> /// <param name="dic">字段名稱,字段中文名稱</param> /// <param name="title">導出第一行標題</param> /// <returns></returns> public Workbook ExportData(DataTable table, Dictionary<string, string> dic, string title = "") { title = string.IsNullOrEmpty(title) ? "導出數據" : title; Workbook workbook = new Workbook(); workbook.Worksheets.RemoveAt(0);//移除第一個sheet var tempStrArray = System.Activator.CreateInstance<T>().GetType().FullName.Split('.');// string tableName = tempStrArray[tempStrArray.Count() - 1];//這兩句是反射生成要操作的表格名稱的, var baseDic = GetColumnsByTable(tableName, ""); foreach (DataColumn item in table.Columns) { string chsColumnName = ""; if (baseDic.TryGetValue(item.ColumnName, out chsColumnName) && !string.IsNullOrEmpty(chsColumnName)) item.ColumnName = chsColumnName; if (dic.TryGetValue(item.ColumnName, out chsColumnName) && !string.IsNullOrEmpty(chsColumnName)) item.ColumnName = chsColumnName; } int Colnum = table.Columns.Count;//表格列數 int Rownum = table.Rows.Count;//表格行數 Worksheet sheet = workbook.Worksheets.Add(title); Cells cells = sheet.Cells;//單元格 //為標題設置樣式 Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增樣式 styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 styleTitle.Font.Name = "宋體";//文字字體 styleTitle.Font.Size = 18;//文字大小 styleTitle.Font.IsBold = true;//粗體 //樣式2 Style style2 = workbook.Styles[workbook.Styles.Add()];//新增樣式 style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style2.Font.Name = "宋體";//文字字體 style2.Font.Size = 13;//文字大小 style2.Font.IsBold = true;//粗體 style2.IsTextWrapped = true;//單元格內容自動換行 style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //樣式3 Style style3 = workbook.Styles[workbook.Styles.Add()];//新增樣式 style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style3.Font.Name = "宋體";//文字字體 style3.Font.Size = 12;//文字大小 style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //生成行1 標題行 cells.Merge(0, 0, 1, Colnum);//合并單元格 cells[0, 0].PutValue(title);//填寫內容 cells[0, 0].SetStyle(styleTitle); cells.SetRowHeight(0, 38); //生成行2 列名行 for (int i = 0; i < Colnum; i++) { cells[1, i].PutValue(table.Columns[i].ColumnName); cells[1, i].SetStyle(style2); cells.SetRowHeight(1, 25); cells.SetColumnWidth(i, 30); } //生成數據行 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[2 + i, k].PutValue(table.Rows[i][k].ToString()); if (k == Colnum - 1) { style3.HorizontalAlignment = TextAlignmentType.Left;//文字居中 } else { style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中 } cells[2 + i, k].SetStyle(style3); } cells.SetRowHeight(2 + i, 24); } return workbook; }
/// <summary> /// 獲取某個表下面的所有列名和說明 /// </summary> /// <param name="tbname">表名</param> /// <param name="orderrule">排序規則</param> /// <returns></returns> public Dictionary<string, string> GetColumnsByTable(string tbname, string orderrule) { StringBuilder sqlsb = new StringBuilder(); sqlsb.Append("SELECT distinct ColumnsName = c.name,Description = isnull(ex.value,'') "); sqlsb.Append("FROM sys.columns c LEFT OUTER JOIN sys.extended_properties ex "); sqlsb.Append("ON ex.major_id = c.object_id AND ex.minor_id = c.column_id AND ex.name = 'MS_Description' "); sqlsb.Append("left outer join systypes t on c.system_type_id=t.xtype "); sqlsb.Append("WHERE OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0 AND "); sqlsb.Append("OBJECT_NAME(c.object_id) ='{0}' "); if (!string.IsNullOrEmpty(orderrule)) { sqlsb.Append("order by ColumnsName {1}"); } else { sqlsb.Append("order by ColumnsName ASC"); } string exsql = string.Format(sqlsb.ToString(), tbname, orderrule); DataTable dt = DB.FromSql(exsql).ToTable() as DataTable;//用了MySoft框架QAQ Dictionary<string, string> dic = new Dictionary<string, string>(); if (dt != null && dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { dic.Add(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString()); } } return dic; }
看完上述內容,你們掌握使用Aspose.Cells怎么實現萬能導出功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。