using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Aspose.Cells; using System.Web; namespace Quiz.WebsiteAnalysis { public class Tool { #region 同一页面分页 /// <summary> /// 分页 /// </summary> /// <param name="PageCount"></param> /// <param name="CurrentIndex"></param> /// <param name="page"></param> /// <param name="param"></param> /// <returns></returns> public static string PageBar(int PageCount, int CurrentIndex, string page, string param = "", string pagename = "page") { string _PageBar = ""; if (PageCount > 1) { _PageBar += "<div class=\"page\">"; if (CurrentIndex == 1) { _PageBar += "<span><上一页</span>"; } else { _PageBar += "<a href=\"" + page + "?" + pagename + "=" + (CurrentIndex - 1).ToString() + "" + param + "\"><上一页</a>"; } int i = 1; if (PageCount <= 10) { for (int time = 0; i + time <= PageCount; time++) { if ((i + time) == CurrentIndex) { _PageBar += "<em>" + (i + time) + "</em>"; } else { _PageBar += "<a href=\"" + page + "?" + pagename + "=" + (i + time) + "" + param + "\">" + (i + time) + "</a>"; } } } else { if (CurrentIndex <= 5) { for (int time = 1; time <= (CurrentIndex == 5 ? 7 : 5); time++) { if ((time) == CurrentIndex) { _PageBar += "<em>" + (time) + "</em>"; } else { _PageBar += "<a href=\"" + page + "?" + pagename + "=" + (time) + "" + param + "\">" + (time) + "</a>"; } } _PageBar += "<em>...</em><a href=\"" + page + "?" + pagename + "=" + PageCount + "" + param + "\">" + PageCount + "</a>"; } if (CurrentIndex > 5) { _PageBar += "<a href=\"" + page + "?" + pagename + "=1" + param + "\">1</a><em>...</em>"; if (CurrentIndex >= PageCount - 5) { for (int time = PageCount - 5; time <= PageCount; time++) { if ((time) == CurrentIndex) { _PageBar += "<em>" + (time) + "</em>"; } else { _PageBar += "<a href=\"" + page + "?" + pagename + "=" + (time) + "" + param + "\">" + (time) + "</a>"; } } } else { for (int time = CurrentIndex; time < CurrentIndex + 5; time++) { if ((time) == CurrentIndex) { _PageBar += "<em>" + (time) + "</em>"; } else { _PageBar += "<a href=\"" + page + "?" + pagename + "=" + (time) + "" + param + "\">" + (time) + "</a>"; } } _PageBar += "<em>...</em><a href=\"" + page + "?" + pagename + "=" + PageCount + "" + param + "\">" + PageCount + "</a>"; } } } if (CurrentIndex == PageCount) { _PageBar += "<span>下一页></span>"; } else { _PageBar += "<a href=\"" + page + "?" + pagename + "=" + (CurrentIndex + 1).ToString() + "" + param + "\">下一页></a>"; } _PageBar += "</div>"; } return _PageBar; } #endregion #region 导出excel /// <summary> /// DataTable导出到Excel /// </summary> /// <param name="dt">DataTable</param> /// <param name="fileName">文件名</param> public static void DataTableToExcel(DataTable dt, string fileName) { DataTableToExcel(dt, fileName, "Sheet1"); } /// <summary> /// DataTable导出到Excel /// </summary> /// <param name="dt">DataTable</param> /// <param name="fileName">文件名</param> /// <param name="sheetName">工作表名</param> public static void DataTableToExcel(DataTable dt, string fileName, string sheetName) { Workbook wb = new Workbook(); Worksheet ws = wb.Worksheets[0]; ws.Name = sheetName; ws.Cells.ImportDataTable(dt, true, 0, 0, dt.Rows.Count, dt.Columns.Count, false, "yyyy-MM-dd"); Style1(wb); Send(wb, fileName); } #region 样式 //默认样式 private static void Style1(Workbook wb) { Worksheet ws = wb.Worksheets[0]; Aspose.Cells.Style titleStyle = wb.Styles[wb.Styles.Add()]; titleStyle.HorizontalAlignment = TextAlignmentType.Center; //水平居中 titleStyle.Font.IsBold = true; //加粗 //titleStyle.Borders.SetStyle(CellBorderType.Thin); //titleStyle.Borders.SetColor(System.Drawing.Color.Black); StyleFlag sf = new StyleFlag(); sf.Borders = true; //标题样式 Range rTitle = ws.Cells.CreateRange(0, 0, 1, ws.Cells.MaxColumn + 1); rTitle.Style = titleStyle; rTitle.RowHeight = 20; //标题高 //加边框 //Range r = ws.Cells.CreateRange(0, 0, ws.Cells.MaxRow + 1, ws.Cells.MaxColumn + 1); //r.ApplyStyle(titleStyle, sf); ws.AutoFilter.SetRange(0, 0, ws.Cells.MaxColumn);//筛选 //ws.FreezePanes(1, 0, 1, 0); //ws.IsGridlinesVisible = false; ws.AutoFitColumns(); } #endregion #region 下载 //下载 private static void Send(Workbook wb, string fileName) { HttpResponse response = HttpContext.Current.Response; response.Clear(); //response.Buffer = true; if (fileName.ToLower().EndsWith("xlsx")) { wb.Save(HttpUtility.UrlEncode(fileName), FileFormatType.Excel2007Xlsx, SaveType.OpenInExcel, response); } else if (fileName.ToLower().EndsWith("xls")) { wb.Save(HttpUtility.UrlEncode(fileName), FileFormatType.Excel2003, SaveType.OpenInExcel, response); } else { Send(wb, fileName + ".xlsx"); } response.Flush(); try { //response.End(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } catch(Exception ex) { } } //下载 private static void Send(WorkbookDesigner wbd, string fileName) { HttpResponse response = HttpContext.Current.Response; response.Clear(); if (fileName.ToLower().EndsWith("xlsx")) { wbd.Save(HttpUtility.UrlEncode(fileName), SaveType.OpenInExcel, FileFormatType.Excel2007Xlsx, response); } else if (fileName.ToLower().EndsWith("xls")) { wbd.Save(HttpUtility.UrlEncode(fileName), SaveType.OpenInExcel, FileFormatType.Excel2003, response); } else { Send(wbd, fileName + ".xlsx"); } response.Flush(); response.End(); } #endregion #endregion } }