標籤:索引 type ++ ott mon 背景 param value export
//轉為pdf private void CelltoPDF(string cellPath, string pdfPath) { Workbook book = new Workbook(); book.Open(cellPath); book.Save(pdfPath, FileFormatType.Pdf); }
//匯入
private DataTable ReadExcel(string fielPathName) { Workbook book = new Workbook(); book.Open(fielPathName); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; //第一列名不匯入 //cells.ExportDataTable(,);
、、
使用 ExportDataTableAsString 轉換成的table標題是string類型,支援和 datatable。select的使用 DataTable dtresult = cells.ExportDataTable(1, 0, cells.MaxDataRow , cells.MaxDataColumn + 1, false); //true會把第一行作為標題,false則 c1,--cn作為標題 dtresult.Columns["Column1"].ColumnName = "Name"; dtresult.Columns["Column2"].ColumnName = "Age"; dtresult.Columns["Column3"].ColumnName = "Gender"; dtresult.Columns["Column4"].ColumnName = "Salary"; dtresult.Columns["Column5"].ColumnName = "BirthDay"; int r = dtresult.Rows.Count; return dtresult; }
//匯出
MemoryStream stream = OutFileToStream(ModelDB.dbDT); byte[] bytes = stream.ToArray();//StreamToBytes(stream); Response.ContentType = "application/octet-stream"; //通知瀏覽器下載檔案而不是開啟 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("23.xls", System.Text.Encoding.UTF8)); Response.BinaryWrite(bytes); Response.Flush(); Response.End();
private MemoryStream OutFileToStream(DataTable dt) { Workbook workbook = new Workbook(); //活頁簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cells = sheet.Cells;//儲存格 //為標題設定樣式 Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增樣式[標題] styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字置中 styleTitle.Font.Name = "宋體";//文字字型 styleTitle.Font.Size = 12;//文字大小 styleTitle.Font.IsBold = true;//粗體 styleTitle.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); styleTitle.Pattern = BackgroundType.Solid;//設定背景顏色 //一般字元串樣式 Aspose.Cells.Style strStyle = workbook.Styles[workbook.Styles.Add()];//新增樣式 strStyle.HorizontalAlignment = TextAlignmentType.Center;//文字置中 strStyle.Font.Name = "宋體";//文字字型 strStyle.Font.Size = 9;//文字大小 strStyle.Font.IsBold = true;//粗體 strStyle.IsTextWrapped = true;//儲存格內容自動換行 //金錢樣式, Aspose.Cells.Style moneyStyle = workbook.Styles[workbook.Styles.Add()];//新增樣式金錢 moneyStyle.Number = 4;//千分位方式展示金錢 //日期樣式 Aspose.Cells.Style dateStyle = workbook.Styles[workbook.Styles.Add()]; dateStyle.Custom = "yyyy-MM-dd"; int Colnum = dt.Columns.Count;//表格列數 int Rownum = dt.Rows.Count;//表格行數 //列名行 //資料庫存標題一般是英文,excel一般是漢字。所以標題一般添加 cells[0, 0].PutValue("姓名_"); cells[0, 0].SetStyle(styleTitle); cells[0, 1].PutValue("年齡_"); cells[0, 1].SetStyle(styleTitle); cells[0, 2].PutValue("性別_"); cells[0, 2].SetStyle(styleTitle); cells[0, 3].PutValue("工資_"); cells[0, 3].SetStyle(styleTitle); cells[0, 4].PutValue("出生日期_"); cells[0, 4].SetStyle(styleTitle); cells.SetRowHeight(0, 35); int[] moneyCol = { 3 };//需要千分位的列縮影 int[] dateCol = { 4 };//需要日期個格式的列索引 //產生列 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { if (dt.Rows[i][k] != null) { if (moneyCol.Contains(k)) { cells[i + 1, k].PutValue(Convert.ToDecimal(dt.Rows[i][k].ToString())); cells[i + 1, k].SetStyle(moneyStyle); } else if (dateCol.Contains(k)) { cells[i + 1, k].PutValue(Convert.ToDateTime(dt.Rows[i][k].ToString())); cells[i + 1, k].SetStyle(dateStyle); } else { cells[i + 1, k].PutValue(dt.Rows[i][k].ToString(), true); cells[i + 1, k].SetStyle(strStyle); } } } cells.SetRowHeight(1 + i, 24); } MemoryStream ms = workbook.SaveToStream(); return ms; }
/// <summary> /// 匯出資料到本地 /// </summary> /// <param name="dt">要匯出的資料</param> /// <param name="tableName">表格標題</param> /// <param name="path">儲存路徑</param> public static void OutFileToDisk(DataTable dt, string tableName, string path) { Workbook workbook = new Workbook(); //活頁簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cells = sheet.Cells;//儲存格 //為標題設定樣式 Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增樣式 styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字置中 styleTitle.Font.Name = "宋體";//文字字型 styleTitle.Font.Size = 18;//文字大小 styleTitle.Font.IsBold = true;//粗體 //樣式2 Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];//新增樣式 style2.HorizontalAlignment = TextAlignmentType.Center;//文字置中 style2.Font.Name = "宋體";//文字字型 style2.Font.Size = 14;//文字大小 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 Aspose.Cells.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; int Colnum = dt.Columns.Count;//表格列數 int Rownum = dt.Rows.Count;//表格行數 //產生行1 標題列 cells.Merge(0, 0, 1, Colnum);//合併儲存格 cells[0, 0].PutValue(tableName);//填寫內容 cells[0, 0].SetStyle(styleTitle); cells.SetRowHeight(0, 38); //產生行2 列名行 for (int i = 0; i < Colnum; i++) { cells[1, i].PutValue(dt.Columns[i].ColumnName); cells[1, i].SetStyle(style2); cells.SetRowHeight(1, 25); } //產生資料行 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[2 + i, k].PutValue(dt.Rows[i][k].ToString()); cells[2 + i, k].SetStyle(style3); } cells.SetRowHeight(2 + i, 24); } workbook.Save(path); }
Aspose 匯出excel小demo