public FileResult Export(string beginTime1, string endTime1, string searchkey, string slaveid) { DateTime dt = DateTime.Now; if (string.IsNullOrEmpty(beginTime1) && string.IsNullOrEmpty(endTime1)) { beginTime1 = dt.ToString("yyyy/MM/01 00:00"); endTime1 = dt.ToString("yyyy/MM/dd 23:59"); } if (string.IsNullOrEmpty(slaveid)) { slaveid = "0"; } byte[] bytes = new byte[1]; List <WaterMeterParas> list = GetAllAttendanceInfos(beginTime1, endTime1, searchkey, slaveid); ITransferData transdata = TransferDataFactory.GetUtil(DataFileType.CSV); DataTable dataTable = new WaterSupplyInfoGetter().GetTable(list); MemoryStream stream = transdata.GetStream(dataTable) as MemoryStream; stream.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(stream, new UTF8Encoding(true)); string strContent = sr.ReadToEnd(); Encoding ecd = new UTF8Encoding(true); bytes = ecd.GetBytes(strContent); byte[] bytesConvent = new byte[bytes.Length + 3]; bytesConvent[0] = 0xEF; bytesConvent[1] = 0xBB; bytesConvent[2] = 0xBF; Array.Copy(bytes, 0, bytesConvent, 3, bytes.Length); return(File(bytesConvent, "application/vnd.ms-excel", Server.UrlEncode(string.Format("用水量信息{0}_{1}.csv", DateTime.Parse(beginTime1).ToString("yyyy-MM-dd-HH-mm-ss"), DateTime.Parse(endTime1).ToString("yyyy-MM-dd-HH-mm-ss"))))); }
public DataTable BindGrid() { string fileName = this.selectDialog.Text; DataTable dt = new DataTable(); if (!string.IsNullOrEmpty(fileName)) { string ext = Path.GetExtension(fileName).ToLower(); FileType fileType = FileHelper.GetFileType(ext); bool isFirstColumn = true; string[] columns = new string[] { "联系人", "号码", "号码类型" }; this.grid1.AutoGenerateColumns = true; this.grid1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; this.grid1.ReadOnly = true; this.grid1.DataSource = null; if (fileType == FileType.Txt) { dt = TxtHelper.ParseDataTable(fileName, columns, isFirstColumn); } else if (fileType == FileType.Excel) { ITransferData t = TransferDataFactory.GetTransferData(fileName); dt = t.GetData(fileName, columns, isFirstColumn); } else { dt = TxtHelper.ToDataTable(AddressBook.Parse(fileName), columns, isFirstColumn); } this.grid1.DataSource = dt; } return(dt); }
/// <summary> /// 解析通讯录文件 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static string Parse(string fileName) { if (!string.IsNullOrEmpty(fileName)) { string ext = Path.GetExtension(fileName).ToLower(); StringBuilder sb = new StringBuilder(); try { string text = FileHelper.GetFileContent(fileName, Encoding.UTF8); FileType fileType = FileHelper.GetFileType(ext); if (!string.IsNullOrEmpty(text)) { bool isFirstColumn = false; string[] columns = new string[] { "联系人", "号码", "号码类型" }; string templ = "{0}\t{1}\t{2}"; if (ext == ".vcf") { sb.AppendLine(string.Format(templ, columns[0], columns[1], columns[2])); string regexName = @"^(?:FN:)(.*)(?:/*.*)$"; MatchCollection matchName = Regex.Matches(text, regexName, RegexOptions.IgnoreCase | RegexOptions.Multiline); string regexTel = @"^(?:TEL;TYPE=CELL(?:;TYPE=PREF)?:)((\+86)?\d{11,12})"; MatchCollection matchTel = Regex.Matches(text, regexTel, RegexOptions.IgnoreCase | RegexOptions.Multiline); if (matchName.Count > 0 && matchTel.Count > 0) { for (int i = 0; i < matchName.Count; i++) { string name = Regex.Replace(matchName[i].Groups[1].Value, "/+.*$", ""); string value = matchTel[i].Groups[1].Value; string type = Regex.IsMatch(value, @"^(\+86){1}\d{11}$") ? "联通" : "移动"; string tel = Regex.Replace(value, @"^(\+86){1}", ""); if (tel.StartsWith("0")) { type = "座机"; } sb.AppendLine(string.Format(templ, name, tel, type)); } } } else if (fileType == FileType.Txt) { sb.Append(text); } else if (fileType == FileType.Excel) { ITransferData transfer = TransferDataFactory.GetTransferData(fileName); DataTable dt = transfer.GetData(fileName, columns, isFirstColumn); sb.Append(TxtHelper.ToTextContent(dt)); } } } catch (Exception ex) { sb.Append(ex.Message); } return(sb.ToString()); } return(string.Empty); }
public ActionResult GetXlsx(int id) { var file = Request.Files[0]; if (file == null) { return(Json(new { code = -1, msg = "上传失败" })); } file.InputStream.Position = 0; var column = string.Empty; var util = TransferDataFactory.GetUtil(file.FileName); var dt = util.GetData(file.InputStream); foreach (var col in dt.Columns) { column += string.IsNullOrEmpty(column) ? col : $"|{col}"; } var cToolM = (ContrastToolModel)Session[ConstSessionKey]; if (cToolM != null) { if (cToolM.UploadFileInfo.Keys.Contains(id)) { cToolM.UploadFileInfo[id] = new KeyValuePair <string, DataTable>(file.FileName, dt); } else { cToolM.UploadFileInfo.Add(id, new KeyValuePair <string, DataTable>(file.FileName, dt)); } } else { column = string.Empty; } var json = Json(new { code = string.IsNullOrEmpty(column) ? -1 : 0, filename = file.FileName, cols = column, msg = "上传成功。" } ); return(json); }
//加载数据 private void btnLoadImport_Click(object sender, EventArgs e) { this.gridImport.AutoGenerateColumns = true; this.gridImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; string fileName = this.selectDialogImport.Text; if (!string.IsNullOrWhiteSpace(fileName)) { ITransferData trans = TransferDataFactory.GetTransferData(fileName); DataTable dt = trans.GetData(fileName, null, true); dt.TableName = Path.GetFileNameWithoutExtension(fileName); this.gridImport.DataSource = dt; } }
private void ExportFile(TransferFileType type, DataTable dt = null) { if (dt == null) { dt = this.filterData1.GetDataSource(); } if (dt != null) { string savePath = Dialog.GetSaveFile(TableName + TransferDataFactory.GetFileExtension(type)); if (!string.IsNullOrEmpty(savePath)) { ITransferData transfer = TransferDataFactory.GetTransferData(type); byte[] bytes = transfer.GetBytes(dt, true); FileHelper.WriteFile(savePath, bytes); if (MsgBox.Confirm("导出完毕,是否打开文件?")) { FileHelper.OpenFile(savePath); } } } }