|
|
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.Append("<meta http-equiv=\"content-type\" content=\"text/csv\"/>");//导出格式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 + ".csv");
|
Response.ContentType = "text/csv";
|
//Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
|
//Response.ContentType = "application/execl";
|
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 "";
|
}
|
}
|