将DataTable的Data到生成CSV文件里面然后在保存到项目文件夹下面
将DataTable的Data到生成CSV文件里面然后在保存到项目文件夹下面呢
C# code?1
怎么做呢,求大神帮忙,
public void ExportToExcelOfficeALLByAdditional() { SearchAdditionalRun search; string excelstr = ""; string name = Request.QueryString["name"]; string date = Request.QueryString["date"]; if (date == "ALL") { search = new SearchAdditionalRun { SearchType = SearchTypeEnum.Total, Status = StatusEnum.Active, }; } else { search = new SearchAdditionalRun { ActualDate = date.ToInteger(), SearchType = SearchTypeEnum.Total, Status = StatusEnum.Active, }; } IList<AdditionalRunInfo> info = iadditionalrun.Search(search); #region AdditionalRunInfo导??出?字??段? string strNull = ""; excelstr = "AdditionalRunID" + "," + "RequestDescription" + "," + "ReasonID" + "," + "Reason" + "," + "DateRaised" + "," + "ExpectedDate" + "," + "ActualDate" + "," + "ProjectID" + "," + "Project #" + "," + "ProjectName" + "," + "InsertTime" + "," + "LastChanged" + "," + "Status" + "\n";//标题 #endregion foreach (AdditionalRunInfo item in info) { #region AdditionalRunInfo导??出?字??段? string DateRaised = item.DateRaised == DateTime.MinValue ? "" : item.DateRaised.ToString("yyyy-MM-dd"); string ExpectedDate = item.ExpectedDate == DateTime.MinValue ? "" : item.ExpectedDate.ToString("yyyy-MM-dd"); string InsertTime = item.InsertTime == DateTime.MinValue ? "" : item.InsertTime.ToString("yyyy-MM-dd"); string LastChanged = item.LastChanged == DateTime.MinValue ? "" : item.LastChanged.ToString("yyyy-MM-dd"); string ProjectNumber = ibkfm.GetBKFM(item.ProjectID).ProjectNumber; string ProjectName = ibkfm.GetBKFM(item.ProjectID).ProjectName; string Reason = isimplecategory.GetSimpleCategoryInfo(item.ReasonID).GetLangInfo(LanguageTypeEnum.Chinese).CategoryName; excelstr += item.AdditionalRunID + "," + StringFormat(item.RequestDescription) + "," + item.ReasonID + "," + Reason + "," + DateRaised + "," + ExpectedDate + "," + date + "," + item.ProjectID + "," + ProjectNumber + "," + ProjectName + "," + InsertTime + "," + LastChanged + "," + item.Status + "\n";//标题 #endregion } string url = Server.MapPath("~\\fileupload\\") + name + ".csv";//要存储的路径 string path = "/fileupload/" + name + ".csv";//导出文件的相对路径,用于下?载 StreamWriter sw = new StreamWriter(url, false, Encoding.GetEncoding("GB2312"));//把文件作为文件流保存在固定的路径 sw.Write(excelstr);// 文件流写出 sw.Flush();//判断是否有文件,如果有则删除 sw.Dispose();//释放流的资源 Response.Write("" + path + "");//输出文件相对路径 }
|