| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Data;
- using System.Collections;
- using System.Text.RegularExpressions;
- namespace XYFDRQ.Common
- {
- public class Tool
- {
- public Tool()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- #region 去除指定字段重复的行
- /// <summary>
- /// 去除指定字段重复的行
- /// </summary>
- /// <param name="dt">需要处理的datatable</param>
- /// <param name="DtFile">检测重复的字段</param>
- /// <returns>处理后的表</returns>
- public static DataTable DelDtSameRow(DataTable dt, params string[] DtFile)
- {
- ArrayList arrList = new ArrayList(); //记录删除重复字段
- List<int> DelRowIndex = new List<int>();//记录删除行在元数据表中的索引值
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string tmpStr = "";
- for (int m = 0; m < DtFile.Length; m++)
- {
- tmpStr += dt.Rows[i][DtFile[m]] + ",";
- }
- tmpStr = tmpStr.Length > 0 ? tmpStr.TrimEnd(',') : "";
- if (arrList.Contains(tmpStr))
- {
- DelRowIndex.Add(i);
- }
- else
- {
- arrList.Add(tmpStr);
- }
- }
- for (int i = 0; i < DelRowIndex.Count; i++)
- {
- dt.Rows.RemoveAt(DelRowIndex[i] - i);
- }
- return dt;
- }
- #endregion
- public static bool CheckMobile(string mobile)
- {
- Regex r = new Regex(@"^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$");
- return r.IsMatch(mobile);
- }
- /// <summary>
- /// DataTable保存到Excel
- /// </summary>
- /// <param name="p_Table"></param>
- /// <param name="p_Response"></param>
- public static void SaveToExcelUtf8(DataTable p_Table, HttpResponse p_Response, string fileName)
- {
- int _CountR = p_Table.Rows.Count;//行数
- int _CountC = p_Table.Columns.Count;//列数
- p_Response.Clear();
- p_Response.Buffer = true;
- p_Response.Charset = "UTF-8";
- p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
- p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(fileName, Encoding.UTF8).ToString());
- p_Response.ContentType = "application/ms-excel";
- //写表头
- for (int i = 0; i < _CountC; i++)
- {
- p_Response.Write(p_Table.Columns[i].ColumnName + "\t");
- }
- p_Response.Write("\n");
- //写表内容
- for (int RowNo = 0; RowNo <= _CountR - 1; RowNo++)
- {
- string RowContent = "";
- for (int CloumnNo = 0; CloumnNo <= _CountC - 1; CloumnNo++)
- {
- RowContent += Convert.ToString(p_Table.Rows[RowNo][CloumnNo]) + "\t";
- }
- RowContent += "\n";
- p_Response.Write(RowContent);
- }
- p_Response.End();
- }
- /// <summary>
- /// DataTable保存到Excel
- /// </summary>
- public static void SaveToExcelGB2312(DataTable p_Table, HttpResponse p_Response, string fileName)
- {
- int _CountR = p_Table.Rows.Count;//行数
- int _CountC = p_Table.Columns.Count;//列数
- p_Response.Clear();
- p_Response.Buffer = true;
- //设置编码
- p_Response.Charset = "GB2312";
- p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
- p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
- p_Response.ContentType = "application/ms-excel";
- //写表头
- for (int i = 0; i < _CountC; i++)
- {
- p_Response.Write(p_Table.Columns[i].ColumnName + "\t");
- }
- p_Response.Write("\n");
- //写表内容
- for (int RowNo = 0; RowNo <= _CountR - 1; RowNo++)
- {
- string RowContent = "";
- for (int CloumnNo = 0; CloumnNo <= _CountC - 1; CloumnNo++)
- {
- RowContent += Convert.ToString(p_Table.Rows[RowNo][CloumnNo]).Replace("\t", "").Replace("\r", "").Replace("\n", "").Replace("\"", "") + "\t";
- }
- RowContent += "\n";
- p_Response.Write(RowContent);
- }
- p_Response.End();
- }
- /// <summary>
- /// 保存数据到Excel表格
- /// </summary>
- /// <param name="gv"></param>
- /// <param name="p_Response"></param>
- /// <param name="fileName"></param>
- public static void SaveDataToExcelGB2312(string content, HttpResponse p_Response, string fileName)
- {
-
- p_Response.Charset = "GB2312";
- p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
- p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
- p_Response.ContentType = "application/ms-excel";
- p_Response.Write(content);
- p_Response.End();
-
- }
- }
- }
|