總結了一下MyXls導出Excle,分頁之后導出全部數據。當然還有一些簡單的方法。但是功能就局限了。
一:(網上搜集)
MyXLS 是一個快速和簡單的讀寫 Excel 文件的 .NET 組件,可用在 ASP.NET 網站和 .NET 應用程序中,無需安裝 Excel 程序,支持 Excel 97 以及以后的版本。
目前MyXls已經實現了單元格(cell)的格式設置,包括文本顏色、文本大小、字體、單位格邊框、底色、列寬、行高,合並單元格,多個sheet頁等功能。以下是MyXLS組件的一些用法:
1.創建一個Excel文檔:
XlsDocument xls =new XlsDocument();
2.創建一個WorkSheet:
Worksheet ws = xls.Workbook.Worksheets.Add("WorkSheet1");
3.指定列格式:
ColumnInfo colInfo =new ColumnInfo(xls, ws);
colInfo.ColumnIndexStart =;
colInfo.ColumnIndexEnd =17;
colInfo.Width =15*256;
ws.AddColumnInfo(colInfo);
列格式必須每次都要重新定義,一個列格式不能重復使用。
4.指定單元格樣式:
XF xf = xls.NewXF();
xf.HorizontalAlignment = HorizontalAlignments.Centered;
xf.VerticalAlignment = VerticalAlignments.Centered;
xf.Pattern =1;
xf.PatternColor = Colors.Default30;
xf.UseBorder =true;
xf.TopLineStyle =1;
xf.TopLineColor = Colors.Black;
xf.BottomLineStyle =1;
xf.BottomLineColor = Colors.Black; xf.LeftLineStyle =1;
xf.LeftLineColor = Colors.Black;
xf.RightLineStyle =1;
xf.RightLineColor = Colors.Black;
xf.Font.Bold =true;
xf.Font.Height =11*20;
xf.Font.ColorIndex =1;
5.給單元格賦值:
ws.Cells.Add(2, 3, "金額(萬元)", xf);
6.合並單元格:
7.MyXls合並單元格有個bug,就是合並后只是第一個單元格有樣式,其余的樣式丟失。所以寫了個函數來合並:
MergeRegion(ref ws, xf, "機構", 1, 1, 2, 1);
publicvoid MergeRegion(ref Worksheet ws, XF xf, string title, int startRow, int startCol, int endRow, int endCol)
{
for (int i = startCol; i <= endCol; i++)
{
for (int j = startRow; j <= endRow; j++)
{
ws.Cells.Add(j, i, title, xf);
}
}
ws.Cells.Merge(startRow, endRow, startCol, endCol); }
雖然效率不怎么樣,但是對於出Excel報表,還OK。
8.指定單元格格式:
cell.Format = StandardFormats.Decimal_1;
具體更多請參考源代碼的StandardFormats類。
9.保存或者發送Excel:
xls.Send(); //或者xls.Save();
二、結合項目
實例:

1 protected void btnExcel_Click(object sender, EventArgs e) 2 { 3 DataTable dt = (DataTable)ViewState["dt"]; 4 string item = "客戶:" + name + " 從" + start + "至" + end + " 采購詳單"; 5 ExcelExport(dt, item); 6 } 7 //先添加引用給出的DLL文件 8 private void ExcelExport(DataTable dt, string item) 9 { 10 11 XlsDocument xls = new XlsDocument(); 12 xls.FileName = "采購詳單.xls";//指定文件名 13 14 Worksheet sheet = xls.Workbook.Worksheets.Add("Inventory"); 15 16 #region 設置各數據列的大小 17 ColumnInfo colInfo = null; 18 int a = 0; 19 int b = 0; 20 //15列 21 for (int i = 0; i < 15; i++) 22 { 23 a = i == 0 ? 0 : (i + 1); 24 b = i == 0 ? 1 : (i + 1); 25 colInfo = new ColumnInfo(xls, sheet); 26 colInfo.ColumnIndexStart = (ushort)a; 27 colInfo.ColumnIndexEnd = (ushort)b; 28 colInfo.Width = 13 * 256; 29 sheet.AddColumnInfo(colInfo); 30 } 31 #endregion 32 33 Cells cells = sheet.Cells; 34 35 #region 合並單元格,得到報表標題 36 //第一行標題 37 MergeArea maTitle = new MergeArea(1, 2, 1, 15); 38 sheet.AddMergeArea(maTitle); 39 XF xfTitle = xls.NewXF(); 40 xfTitle.HorizontalAlignment = HorizontalAlignments.Centered; 41 xfTitle.VerticalAlignment = VerticalAlignments.Centered; 42 xfTitle.Font.FontName = "宋體"; 43 xfTitle.Font.Height = 16 * 20; 44 xfTitle.Font.Bold = true; 45 cells.Add(1, 1, "采購訂單明細", xfTitle); 46 #endregion 47 48 //第二行描述 49 MergeArea maTime1 = new MergeArea(3, 3, 1, 15); 50 sheet.AddMergeArea(maTime1); 51 XF xfTopBar = xls.NewXF(); 52 xfTopBar.Font.FontName = "宋體"; 53 //item為描述內容 54 cells.Add(3, 1, item, xfTopBar); 55 56 #region 設置Excel數據列標題的格式 57 XF xfDataHead = xls.NewXF(); 58 xfDataHead.HorizontalAlignment = HorizontalAlignments.Centered; 59 xfDataHead.VerticalAlignment = VerticalAlignments.Centered; 60 xfDataHead.Font.FontName = "宋體"; 61 xfDataHead.Font.Bold = true; 62 xfDataHead.UseBorder = true; 63 xfDataHead.BottomLineStyle = 1; 64 xfDataHead.BottomLineColor = Colors.Black; 65 xfDataHead.TopLineStyle = 1; 66 xfDataHead.TopLineColor = Colors.Black; 67 xfDataHead.LeftLineStyle = 1; 68 xfDataHead.LeftLineColor = Colors.Black; 69 xfDataHead.RightLineStyle = 1; 70 xfDataHead.RightLineColor = Colors.Black; 71 #endregion 72 73 #region 添加列標題 74 cells.Add(4, 1, "單據號", xfDataHead); 75 cells.Add(4, 2, "產品名稱", xfDataHead); 76 cells.Add(4, 3, "規格", xfDataHead); 77 cells.Add(4, 4, "厚度", xfDataHead); 78 cells.Add(4, 5, "等級", xfDataHead); 79 cells.Add(4, 6, "單位", xfDataHead); 80 cells.Add(4, 7, "數量", xfDataHead); 81 cells.Add(4, 8, "過度", xfDataHead); 82 cells.Add(4, 9, "包裝", xfDataHead); 83 cells.Add(4, 10, "銷售區域", xfDataHead); 84 cells.Add(4, 11, "顏色", xfDataHead); 85 cells.Add(4, 12, "單位代碼", xfDataHead); 86 cells.Add(4, 13, "包裝代碼", xfDataHead); 87 cells.Add(4, 14, "單價", xfDataHead); 88 cells.Add(4, 15, "單價", xfDataHead); 89 #endregion 90 91 #region 設置各數據列的格式 92 93 XF xfData = xls.NewXF(); 94 xfData.Font.FontName = "宋體"; 95 xfData.UseBorder = true; 96 xfData.BottomLineStyle = 1; 97 xfData.BottomLineColor = Colors.Black; 98 xfData.TopLineStyle = 1; 99 xfData.TopLineColor = Colors.Black; 100 xfData.LeftLineStyle = 1; 101 xfData.LeftLineColor = Colors.Black; 102 xfData.RightLineStyle = 1; 103 xfData.RightLineColor = Colors.Black; 104 #endregion 105 106 #region 填充數據 107 108 int j = 5;//從第五行開始為數據行 109 110 double provideTotal = 0; 111 112 double useTotal = 0; 113 114 double lossTotal = 0; 115 116 foreach (DataRow dr in dt.Rows) 117 { 118 119 cells.Add(j, 1, dr["djno"], xfData); 120 cells.Add(j, 2, dr["itemname"], xfData); 121 cells.Add(j, 3, dr["specs"], xfData); 122 cells.Add(j, 4, dr["ply"], xfData); 123 cells.Add(j, 5, dr["rank"], xfData); 124 cells.Add(j, 6, dr["ut"], xfData); 125 cells.Add(j, 7, dr["num"], xfData); 126 cells.Add(j, 8, dr["transcolor"], xfData); 127 cells.Add(j, 9, dr["packname"], xfData); 128 cells.Add(j, 10, dr["salesarea"], xfData); 129 cells.Add(j, 11, dr["color"], xfData); 130 cells.Add(j, 12, dr["utno"], xfData); 131 cells.Add(j, 13, dr["packno"], xfData); 132 cells.Add(j, 14, dr["zkprice"], xfData); 133 cells.Add(j, 15, dr["zkamt"], xfData); 134 j++; 135 136 } 137 #endregion 138 string path = Server.MapPath("~/"); 139 xls.Save(path + "\\excel\\", true); 140 string down = Server.MapPath("~/") + "\\excel\\" + xls.FileName; 141 FileInfo DownloadFile = new FileInfo(down); 142 Response.Clear(); 143 Response.ClearHeaders(); 144 Response.Buffer = false; 145 Response.ContentType = "application/octet-stream"; 146 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(xls.FileName, System.Text.Encoding.UTF8)); 147 Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); 148 Response.WriteFile(DownloadFile.FullName); 149 Response.Flush(); 150 Response.End(); 151 //xls.Send(); 152 }
效果圖下載:效果