MyXls類庫導(dǎo)出Excel的各種設(shè)置
MyXls是一個操作Excel的開源類庫,支持設(shè)置字體、列寬、行高(由BOSSMA實(shí)現(xiàn))、合并單元格、邊框、背景顏色、數(shù)據(jù)類型、自動換行、對齊方式等,通過眾多項(xiàng)目的使用表現(xiàn),證明MyXls對于創(chuàng)建簡單格式的Excel文件十分快捷方便。
本文將通過實(shí)例的方式詳細(xì)說明如何通過各種屬性設(shè)置MyXls的樣式。
// 準(zhǔn)備測試數(shù)據(jù) List<PersonInfo> list = new List<PersonInfo>(); for (int i = 1; i <= 200; i ) { PersonInfo person = new PersonInfo() { RealName = "張" i, Gender = (i % 2 == 0 ? "男" : "女"), Age = 20 (i % 3) }; list.Add(person); } int recordCount = 200; // 要導(dǎo)出的記錄總數(shù) int maxRecordCount = 100; // 每個sheet表的最大記錄數(shù) int sheetCount = 1; // Sheet表的數(shù)目 XlsDocument xls = new XlsDocument(); xls.FileName = "MyXls-" DateTime.Now.ToString("yyyyMMddHHmmss") ".xls"; // 計(jì)算需要多少個sheet表顯示數(shù)據(jù) if (recordCount > maxRecordCount) { sheetCount = (int)Math.Ceiling((decimal)recordCount / (decimal)maxRecordCount); } // Sheet標(biāo)題樣式 XF titleXF = xls.NewXF(); // 為xls生成一個XF實(shí)例,XF是單元格格式對象 titleXF.HorizontalAlignment = HorizontalAlignments.Centered; // 設(shè)定文字居中 titleXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中 titleXF.UseBorder = true; // 使用邊框 titleXF.TopLineStyle = 1; // 上邊框樣式 titleXF.TopLineColor = Colors.Black; // 上邊框顏色 titleXF.LeftLineStyle = 1; // 左邊框樣式 titleXF.LeftLineColor = Colors.Black; // 左邊框顏色 titleXF.RightLineStyle = 1; // 右邊框樣式 titleXF.RightLineColor = Colors.Black; // 右邊框顏色 titleXF.Font.FontName = "宋體"; // 字體 titleXF.Font.Bold = true; // 是否加楚 titleXF.Font.Height = 12 * 20; // 字大?。ㄗ煮w大小是以 1/20 point 為單位的) // 列標(biāo)題樣式 XF columnTitleXF = xls.NewXF(); // 為xls生成一個XF實(shí)例,XF是單元格格式對象 columnTitleXF.HorizontalAlignment = HorizontalAlignments.Centered; // 設(shè)定文字居中 columnTitleXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中 columnTitleXF.UseBorder = true; // 使用邊框 columnTitleXF.TopLineStyle = 1; // 上邊框樣式 columnTitleXF.TopLineColor = Colors.Black; // 上邊框顏色 columnTitleXF.BottomLineStyle = 1; // 下邊框樣式 columnTitleXF.BottomLineColor = Colors.Black; // 下邊框顏色 columnTitleXF.LeftLineStyle = 1; // 左邊框樣式 columnTitleXF.LeftLineColor = Colors.Black; // 左邊框顏色 columnTitleXF.Pattern = 1; // 單元格填充風(fēng)格。如果設(shè)定為0,則是純色填充(無色),1代表沒有間隙的實(shí)色 columnTitleXF.PatternBackgroundColor = Colors.Red; // 填充的底色 columnTitleXF.PatternColor = Colors.Default2F; // 填充背景色 // 數(shù)據(jù)單元格樣式 XF dataXF = xls.NewXF(); // 為xls生成一個XF實(shí)例,XF是單元格格式對象 dataXF.HorizontalAlignment = HorizontalAlignments.Centered; // 設(shè)定文字居中 dataXF.VerticalAlignment = VerticalAlignments.Centered; // 垂直居中 dataXF.UseBorder = true; // 使用邊框 dataXF.LeftLineStyle = 1; // 左邊框樣式 dataXF.LeftLineColor = Colors.Black; // 左邊框顏色 dataXF.BottomLineStyle = 1; // 下邊框樣式 dataXF.BottomLineColor = Colors.Black; // 下邊框顏色 dataXF.Font.FontName = "宋體"; dataXF.Font.Height = 9 * 20; // 設(shè)定字大?。ㄗ煮w大小是以 1/20 point 為單位的) dataXF.UseProtection = false; // 默認(rèn)的就是受保護(hù)的,導(dǎo)出后需要啟用編輯才可修改 dataXF.TextWrapRight = true; // 自動換行 // 遍歷創(chuàng)建Sheet for (int i = 1; i <= sheetCount; i ) { // 根據(jù)計(jì)算出來的Sheet數(shù)量,一個個創(chuàng)建 // 行和列的設(shè)置需要添加到指定的Sheet中,且每個設(shè)置對象不能重用(因?yàn)榭梢栽O(shè)置起始和終止行或列,就沒有太大必要重用了,這應(yīng)是一個策略問題) Worksheet sheet; if (sheetCount == 1) { sheet = xls.Workbook.Worksheets.Add("人員信息表"); } else { sheet = xls.Workbook.Worksheets.Add("人員信息表 - " i); } // 序號列設(shè)置 ColumnInfo col0 = new ColumnInfo(xls, sheet); // 列對象 col0.ColumnIndexStart = 0; // 起始列為第1列,索引從0開始 col0.ColumnIndexEnd = 0; // 終止列為第1列,索引從0開始 col0.Width = 8 * 256; // 列的寬度計(jì)量單位為 1/256 字符寬 sheet.AddColumnInfo(col0); // 把格式附加到sheet頁上 // 姓名列設(shè)置 ColumnInfo col1 = new ColumnInfo(xls, sheet); // 列對象 col1.ColumnIndexStart = 1; // 起始列為第2列,索引從0開始 col1.ColumnIndexEnd = 1; // 終止列為第2列,索引從0開始 col1.Width = 16 * 256; // 列的寬度計(jì)量單位為 1/256 字符寬 sheet.AddColumnInfo(col1); // 把格式附加到sheet頁上 // 性別列設(shè)置 ColumnInfo col2 = new ColumnInfo(xls, sheet); // 列對象 col2.ColumnIndexStart = 2; // 起始列為第3列,索引從0開始 col2.ColumnIndexEnd = 2; // 終止列為第3列,索引從0開始 col2.Width = 16 * 256; // 列的寬度計(jì)量單位為 1/256 字符寬 sheet.AddColumnInfo(col2); // 把格式附加到sheet頁上 // 年齡列設(shè)置 ColumnInfo col3 = new ColumnInfo(xls, sheet); // 列對象 col3.ColumnIndexStart = 3; // 起始列為第4列,索引從0開始 col3.ColumnIndexEnd = 3; // 終止列為第4列,索引從0開始 col3.Width = 16 * 256; // 列的寬度計(jì)量單位為 1/256 字符寬 sheet.AddColumnInfo(col3); // 把格式附加到sheet頁上 // 行設(shè)置 RowInfo rol1 = new RowInfo(); // 行對象 rol1.RowHeight = 16 * 20; // 行高 rol1.RowIndexStart = 3; // 行設(shè)置起始列,索引從1開始 rol1.RowIndexEnd = (ushort)(maxRecordCount 2); //行設(shè)置結(jié)束列 sheet.AddRowInfo(rol1); // 把設(shè)置附加到sheet頁上 // 合并單元格 //sheet.Cells.Merge(1, 1, 1, 4); MergeArea titleArea = new MergeArea(1, 1, 1, 4); // 一個合并單元格實(shí)例(合并第1行、第1列 到 第1行、第4列) sheet.AddMergeArea(titleArea); //填加合并單元格 // 開始填充數(shù)據(jù)到單元格 Cells cells = sheet.Cells; // Sheet標(biāo)題行,行和列的索引都是從1開始的 Cell cell = cells.Add(1, 1, "人員信息統(tǒng)計(jì)表", titleXF); cells.Add(1, 2, "", titleXF); // 合并單元格后仍需要設(shè)置每一個單元格,樣式才有效 cells.Add(1, 3, "", titleXF); // 合并單元格后仍需要設(shè)置每一個單元格,樣式才有效 cells.Add(1, 4, "", titleXF); // 合并單元格后仍需要設(shè)置每一個單元格,樣式才有效 sheet.Rows[1].RowHeight = 40 * 20; // 對指定的行設(shè)置行高 // 列標(biāo)題行 cells.Add(2, 1, "序號", columnTitleXF); cells.Add(2, 2, "姓名", columnTitleXF); cells.Add(2, 3, "性別", columnTitleXF); // 最右側(cè)的列需要右邊框,通過修改樣式columnTitleXF的方式,還可以通過設(shè)置單元格屬性的方式實(shí)現(xiàn)。 columnTitleXF.RightLineStyle = 1; columnTitleXF.RightLineColor = Colors.Black; cells.Add(2, 4, "年齡", columnTitleXF); sheet.Rows[2].RowHeight = 18 * 20; // 對指定的行設(shè)置行高 // 行索引 int rowIndex = 3; for (int j = 0; j < maxRecordCount; j ) { // 當(dāng)前記錄在數(shù)據(jù)集合中的索引 int k = (i - 1) * maxRecordCount j; // 如果達(dá)到sheet最大記錄數(shù)則跳出 if (k >= recordCount) { break; } // 設(shè)置單元格的值 cells.Add(rowIndex, 1, k 1, dataXF); cells.Add(rowIndex, 2, list[k].RealName, dataXF); cells.Add(rowIndex, 3, list[k].Gender, dataXF); // 最右側(cè)的列需要右邊框,通過給Cell設(shè)置屬性的方式實(shí)現(xiàn),因?yàn)椴⒉皇撬械膯卧穸夹枰O(shè)置,不能通過修改樣式dataXF的方式 Cell lastCell = cells.Add(rowIndex, 4, list[k].Age, dataXF); lastCell.RightLineStyle = 1; lastCell.RightLineColor = Colors.Black; // 行號遞增 rowIndex ; } } // 在瀏覽器中輸出Excel文件 xls.Send();
原文鏈接:MyXls類庫導(dǎo)出Excel的各種設(shè)置