wangsheng
2022-03-22 e1190c55b49d8f3ee89fa1bd715aa579395d98ed
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
 
 
using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public class WebTool
{
  public void BindTree(DataTable dataSource, TreeNodeCollection nodes, string parentId, string idColumn, string parentIdColumn, string textColumn, string values)
  {
    nodes.Clear();
    this.BindTreeNode(dataSource, nodes, parentId, idColumn, parentIdColumn, textColumn, values);
  }
 
  public void BindTreeNode(DataTable dataSource, TreeNodeCollection nodes, string parentId, string idColumn, string parentIdColumn, string textColumn, string values)
  {
    foreach (DataRowView dataRowView in new DataView(dataSource)
    {
      RowFilter = string.IsNullOrEmpty(parentId) ? parentIdColumn + " is null" : string.Format(parentIdColumn + "='{0}'", (object) parentId)
    })
    {
      TreeNode treeNode = new TreeNode();
      treeNode.Value = dataRowView[idColumn].ToString();
      treeNode.Text = dataRowView[textColumn].ToString();
      treeNode.Checked = (Enumerable.Any<string>((IEnumerable<string>) values.Split(new char[1]
      {
        ','
      }, StringSplitOptions.RemoveEmptyEntries), (Func<string, bool>) (c => c == treeNode.Value)) ? 1 : 0) != 0;
      treeNode.ShowCheckBox = new bool?(true);
      nodes.Add(treeNode);
      this.BindTree(dataSource, treeNode.ChildNodes, treeNode.Value, idColumn, parentIdColumn, textColumn, values);
    }
  }
 
  public void Export(DataTable dataTable, Dictionary<string, string> dictionary, string fileName, HttpResponse Response)
  {
    StringBuilder stringBuilder = new StringBuilder();
    //stringBuilder.Append("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");//旧的-导出xls
    stringBuilder.Append("<meta http-equiv=\"content-type\" content=\"vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8\"/>");//导出格式xlsx
    stringBuilder.AppendLine("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
    stringBuilder.AppendLine("<tr>");
    foreach (KeyValuePair<string, string> keyValuePair in dictionary)
    {
      stringBuilder.AppendLine("<th>");
      stringBuilder.Append(keyValuePair.Key);
      stringBuilder.Append("</th>");
    }
    stringBuilder.Append("</tr>");
    if (dataTable.Rows.Count == 0)
    {
      stringBuilder.Append("<tr>");
      stringBuilder.AppendLine("<td  colspan=\"" + (object) dictionary.Count + "\">");
      stringBuilder.Append("暂无数据");
      stringBuilder.Append("</td>");
      stringBuilder.Append("</tr>");
    }
    foreach (DataRow dataRow in (InternalDataCollectionBase) dataTable.Rows)
    {
      stringBuilder.Append("<tr>");
      foreach (KeyValuePair<string, string> keyValuePair in dictionary)
      {
        string str = dataRow[keyValuePair.Value].ToString();
        stringBuilder.AppendLine("<td>");
        stringBuilder.Append(str);
        stringBuilder.Append("</td>");
      }
      stringBuilder.Append("</tr>");
    }
    stringBuilder.AppendLine("</table>");
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
    Response.ContentType = "application/excel";
    Response.ContentEncoding = Encoding.UTF8;
    Response.Write(stringBuilder.ToString());
    Response.End();
  }
 
  public void Export(DataTable dataTable, List<string> columnName, List<string> columnChinaName, List<int> columnWidth, string reportTitleName, string reportFileName, HttpResponse Response)
  {
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    Cells cells = worksheet.Cells;
    string[,] strArray = new string[dataTable.Rows.Count, columnName.Count];
    for (int index1 = 0; index1 < dataTable.Rows.Count; ++index1)
    {
      for (int index2 = 0; index2 < columnName.Count; ++index2)
        strArray[index1, index2] = dataTable.Rows[index1][columnName[index2].ToString() ?? ""].ToString();
    }
    Aspose.Cells.Style style1 = workbook.Styles[workbook.Styles.Add()];
    style1.HorizontalAlignment = TextAlignmentType.Center;
    style1.Font.Name = "宋体";
    style1.Font.IsBold = true;
    style1.Font.Size = 18;
    cells["A1"].SetStyle(style1);
    cells["A1"].PutValue(reportTitleName);
    cells.SetRowHeight(0, 20.0);
    cells.CreateRange(0, 0, 1, columnName.Count).Merge();
    Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];
    style2.HorizontalAlignment = TextAlignmentType.Center;
    style2.Font.Name = "宋体";
    style2.Font.Size = 15;
    for (int columnIndex = 0; columnIndex < columnChinaName.Count; ++columnIndex)
    {
      cells[1, columnIndex].PutValue(columnChinaName[columnIndex].ToString());
      cells[1, columnIndex].SetStyle(style2);
      worksheet.AutoFitColumn(columnIndex);
    }
    Aspose.Cells.Style style3 = workbook.Styles[workbook.Styles.Add()];
    style3.HorizontalAlignment = TextAlignmentType.Left;
    style3.Font.Name = "宋体";
    style3.Font.Size = 12;
    for (int index1 = 0; index1 < strArray.Length / columnName.Count; ++index1)
    {
      for (int index2 = 0; index2 < columnName.Count; ++index2)
      {
        cells[index1 + 2, index2].PutValue(strArray[index1, index2].ToString());
        cells[index1 + 2, index2].SetStyle(style3);
      }
    }
    for (int column = 0; column < columnName.Count; ++column)
    {
      if (columnWidth == null)
        cells.SetColumnWidth(column, 10.0);
      else
        cells.SetColumnWidth(column, Convert.ToDouble(columnWidth[column].ToString()));
    }
    Response.ContentType = "application/ms-excel;charset=utf-8";
    Response.AddHeader("content-disposition", "attachment; filename=" + string.Format("{0}.xls", (object) reportFileName));
    MemoryStream memoryStream = workbook.SaveToStream();
    Response.BinaryWrite(memoryStream.ToArray());
    Response.End();
  }
 
  public void Export(DataTable dataTable, List<string> columnName, List<string> columnChinaName, List<int> columnWidth, string reportFileName, HttpResponse Response)
  {
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    Cells cells = worksheet.Cells;
    string[,] strArray = new string[dataTable.Rows.Count, columnName.Count];
    for (int index1 = 0; index1 < dataTable.Rows.Count; ++index1)
    {
      for (int index2 = 0; index2 < columnName.Count; ++index2)
        strArray[index1, index2] = dataTable.Rows[index1][columnName[index2].ToString() ?? ""].ToString();
    }
    Aspose.Cells.Style style1 = workbook.Styles[workbook.Styles.Add()];
    style1.HorizontalAlignment = TextAlignmentType.Center;
    style1.Font.Name = "宋体";
    style1.Font.Size = 13;
    for (int columnIndex = 0; columnIndex < columnChinaName.Count; ++columnIndex)
    {
      cells[0, columnIndex].PutValue(columnChinaName[columnIndex].ToString());
      cells[0, columnIndex].SetStyle(style1);
      worksheet.AutoFitColumn(columnIndex);
    }
    Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];
    style2.HorizontalAlignment = TextAlignmentType.Left;
    style2.Font.Name = "宋体";
    style2.Font.Size = 12;
    for (int index1 = 0; index1 < strArray.Length / columnName.Count; ++index1)
    {
      for (int index2 = 0; index2 < columnName.Count; ++index2)
      {
        cells[index1 + 1, index2].PutValue(strArray[index1, index2].ToString());
        cells[index1 + 1, index2].SetStyle(style2);
      }
    }
    for (int column = 0; column < columnName.Count; ++column)
    {
      if (columnWidth == null)
        cells.SetColumnWidth(column, 10.0);
      else
        cells.SetColumnWidth(column, Convert.ToDouble(columnWidth[column].ToString()));
    }
    Response.ContentType = "application/ms-excel;charset=utf-8";
    Response.AddHeader("content-disposition", "attachment; filename=" + string.Format("{0}.xls", (object) reportFileName));
    MemoryStream memoryStream = workbook.SaveToStream();
    Response.BinaryWrite(memoryStream.ToArray());
    Response.End();
  }
 
  public void Export(DataSet dataSet, List<string> sheetName, List<string> columnName, List<string> columnChinaName, List<int> columnWidth, string reportFileName, HttpResponse Response)
  {
    Workbook workbook = new Workbook();
    workbook.Worksheets.RemoveAt(0);
    Aspose.Cells.Style style1 = workbook.Styles[workbook.Styles.Add()];
    style1.HorizontalAlignment = TextAlignmentType.Center;
    style1.Font.Name = "宋体";
    style1.Font.Size = 13;
    Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];
    style2.HorizontalAlignment = TextAlignmentType.Left;
    style2.Font.Name = "宋体";
    style2.Font.Size = 12;
    for (int index1 = 0; index1 < dataSet.Tables.Count; ++index1)
    {
      Worksheet worksheet = workbook.Worksheets.Add(sheetName[index1]);
      Cells cells = worksheet.Cells;
      string[,] strArray = new string[dataSet.Tables[index1].Rows.Count, columnName.Count];
      for (int index2 = 0; index2 < dataSet.Tables[index1].Rows.Count; ++index2)
      {
        for (int index3 = 0; index3 < columnName.Count; ++index3)
          strArray[index2, index3] = dataSet.Tables[index1].Rows[index2][columnName[index3].ToString() ?? ""].ToString();
      }
      for (int columnIndex = 0; columnIndex < columnChinaName.Count; ++columnIndex)
      {
        cells[0, columnIndex].PutValue(columnChinaName[columnIndex].ToString());
        cells[0, columnIndex].SetStyle(style1);
        worksheet.AutoFitColumn(columnIndex);
      }
      for (int index2 = 0; index2 < strArray.Length / columnName.Count; ++index2)
      {
        for (int index3 = 0; index3 < columnName.Count; ++index3)
        {
          cells[index2 + 1, index3].PutValue(strArray[index2, index3].ToString());
          cells[index2 + 1, index3].SetStyle(style2);
        }
      }
      for (int column = 0; column < columnName.Count; ++column)
      {
        if (columnWidth == null)
          cells.SetColumnWidth(column, 10.0);
        else
          cells.SetColumnWidth(column, Convert.ToDouble(columnWidth[column].ToString()));
      }
    }
    Response.ContentType = "application/ms-excel;charset=utf-8";
    Response.AddHeader("content-disposition", "attachment; filename=" + string.Format("{0}.xls", (object) reportFileName));
    MemoryStream memoryStream = workbook.SaveToStream();
    Response.BinaryWrite(memoryStream.ToArray());
    Response.End();
  }
 
  public DataSet ReadExcel(string path)
  {
    string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;";
    using (OleDbConnection oleDbConnection = new OleDbConnection(str))
    {
      oleDbConnection.Open();
      DataSet dataSet = new DataSet();
      DataTable schema = oleDbConnection.GetSchema("Tables");
      for (int index = 0; index < schema.Rows.Count; ++index)
      {
        DataTable dataTable = new DataTable();
        new OleDbDataAdapter("select * from [" + schema.Rows[index]["TABLE_NAME"].ToString() + "]", str).Fill(dataTable);
        dataSet.Tables.Add(dataTable);
      }
      return dataSet;
    }
  }
 
  public void LogAdd(string title, string description, int parameterId)
  {
  }
 
  public void LogAdd(Page page, Exception e)
  {
  }
 
  public void DeleteUploadFile(HttpContext context, List<string> files)
  {
    foreach (string fileName in files)
      this.DeleteUploadFile(context, fileName);
  }
 
  public void DeleteUploadFile(HttpContext context, string fileName)
  {
    if (string.IsNullOrEmpty(fileName) || fileName.ToLower().StartsWith("/file") || fileName.ToLower().StartsWith("file"))
      return;
    fileName = context.Server.MapPath("file/" + fileName);
    if (!File.Exists(fileName))
      return;
    File.Delete(fileName);
  }
 
  public bool CheckExtensionName(HttpRequest request, string fieldName)
  {
    return this.CheckExtensionName(request, fieldName, new string[5]
    {
      ".gif",
      ".jpg",
      ".jpeg",
      ".png",
      ".bmp"
    });
  }
 
  public bool CheckExtensionName(HttpRequest request, string fieldName, string[] extensions)
  {
    if (Enumerable.Contains<string>((IEnumerable<string>) request.Files.AllKeys, fieldName))
    {
      HttpPostedFile httpPostedFile = request.Files[fieldName];
      if (httpPostedFile.ContentLength > 0)
      {
        string str1 = Path.GetExtension(httpPostedFile.FileName).ToLower();
        foreach (string str2 in extensions)
        {
          if (str1 == str2)
            return true;
        }
      }
    }
    return false;
  }
 
  public string SaveUploadFile(HttpContext context, string fieldName)
  {
    HttpRequest request = context.Request;
    if (Enumerable.Contains<string>((IEnumerable<string>) request.Files.AllKeys, fieldName))
    {
      HttpPostedFile httpPostedFile = request.Files[fieldName];
      if (httpPostedFile.ContentLength > 0)
      {
        DateTime now = DateTime.Now;
        string str1 = Path.GetExtension(httpPostedFile.FileName).ToLower();
        string str2 = now.ToString("yyyyMMddHHmmssfff") + str1;
        string path1 = ConfigurationManager.AppSettings["UploadFilePath"];
        string str3 = context.Server.MapPath(path1);
        if ((int) str3[str3.Length - 1] != 92)
          str3 = str3 + str3 + "\\";
        string path2 = str3 + now.ToString("yyyy-MM") + "\\";
        if (!Directory.Exists(path2))
          Directory.CreateDirectory(path2);
        httpPostedFile.SaveAs(path2 + str2);
        return now.ToString("yyyy-MM") + "/" + str2;
      }
    }
    return "";
  }
}