關於MVC將CSV格式文件保存到指定地址的BUG修正
前些天發的博文在本機測試的時候沒有問題,經發布後,程序地址發生變化,導致不能正常完成導出,現修改程序如下:
前端:
function ExportOut() {
window.open('/StoreManage/Export');
};
後端代碼:(當然是在Controllers中)
public FileContentResult Export()
{
System.IO.MemoryStream output = new System.IO.MemoryStream();
string excelstr = "title,address,longitude,latitude,coord_type,,AreaHead,StoreCorde,StoreRemarks,StoreDescription,StoreClass,StoreHead,StoreTel" + "\n";
var list = storeServices.LoadEntites(o =>o.IsDelete==false).ToList();
//輸出內容
foreach (Store items in list)
{
string StoreAddress = string.IsNullOrEmpty(items.StoreAddress) ? "" : items.StoreAddress.Trim();
string StoreName = string.IsNullOrEmpty(items.StoreName) ? "" : items.StoreName.Trim();
string StoreTel1 = string.IsNullOrEmpty(items.StoreTel1) ? "" : items.StoreTel1.Trim();
excelstr +=
StoreName + "," +
StoreAddress + "," +
items.StoreLongitude + "," +
items.StoreLatitude + "," +
"3,," + //3:百度地理座標加密方式
items.AreaHead + "," +
items.StoreCode + "," +
items.StoreRemarks + "," +
items.StoreDescription + "," +
items.StoreClass + "," +
items.StoreHead + "," +
StoreTel1 + "\n";
}
try
{
byte[] fileContent = System.Text.Encoding.GetEncoding("GB2312").GetBytes(excelstr);
return File(fileContent, "application/octet-stream", "門店信息.csv");
}
catch (Exception ex)
{
throw ex;
}
}
說明:要注意引用 IO空間
前端:
function ExportOut() {
window.open('/StoreManage/Export');
};
後端代碼:(當然是在Controllers中)
public FileContentResult Export()
{
System.IO.MemoryStream output = new System.IO.MemoryStream();
string excelstr = "title,address,longitude,latitude,coord_type,,AreaHead,StoreCorde,StoreRemarks,StoreDescription,StoreClass,StoreHead,StoreTel" + "\n";
var list = storeServices.LoadEntites(o =>o.IsDelete==false).ToList();
//輸出內容
foreach (Store items in list)
{
string StoreAddress = string.IsNullOrEmpty(items.StoreAddress) ? "" : items.StoreAddress.Trim();
string StoreName = string.IsNullOrEmpty(items.StoreName) ? "" : items.StoreName.Trim();
string StoreTel1 = string.IsNullOrEmpty(items.StoreTel1) ? "" : items.StoreTel1.Trim();
excelstr +=
StoreName + "," +
StoreAddress + "," +
items.StoreLongitude + "," +
items.StoreLatitude + "," +
"3,," + //3:百度地理座標加密方式
items.AreaHead + "," +
items.StoreCode + "," +
items.StoreRemarks + "," +
items.StoreDescription + "," +
items.StoreClass + "," +
items.StoreHead + "," +
StoreTel1 + "\n";
}
try
{
byte[] fileContent = System.Text.Encoding.GetEncoding("GB2312").GetBytes(excelstr);
return File(fileContent, "application/octet-stream", "門店信息.csv");
}
catch (Exception ex)
{
throw ex;
}
}
說明:要注意引用 IO空間
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.