Tool.cs
8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
}
}