我想从asp.net mvc中的数据中获取Excel文件报告, 但当我在本地主机并从我的代码运行时,它正确地给我一个文件并将文件打开正确。 但是当我在服务器中获取代码并导出Excel文件时,在下载文件后,我单击“打开”,但它有错误,文件格式不正确且无法打开。我很迷茫 。请帮我 。我的代码:
public FileResult ExportOrders(string sortOrder, string title, string orderStatus
, string sellerTrackCode, string paymentType, string settlementType, string sellerId, string excelDocumentId,
string postmanId, string sellerCustomerName, string submittedDate, string fromSubmittedDate, string tillSubmittedDate,
string fromDeliveredDate, string tillDeliveredDate, string fromPickUpDate, string tillPickUpDate, string fromId,
string tillId, int? menuType, int? page, int? pageSize)
{
var query = _orderRepository.AsQueryable();
var currentUser = User.Identity.GetUserName();
if (currentUser != string.Empty)
{
var dbUser = _personRoleRepository.AsQueryable().FirstOrDefault(x => x.Person.UserName == currentUser);
if (dbUser != null)
{
List<SellerCustomer> allSellerCustomers = new List<SellerCustomer>();
var role = dbUser.Role;
if (role.Title == "Seller")
{
var seller = _sellerRepository.AsQueryable().First(x => x.PersonId == dbUser.PersonId);
query = query.Where(x => x.SellerId == seller.Id);
}
}
}
if (menuType == (int)MenuType.UnDoneOrders)
{
query = query.Where(x => x.OrderStatus == (byte)OrderStatus.Submitted || x.OrderStatus == (byte)OrderStatus.PayedByUser ||
x.OrderStatus == (byte)OrderStatus.TransmitToPort);
ViewBag.menuType = MenuType.UnDoneOrders;
}
if (menuType == (int)MenuType.OpenOrders)
{
query = query.Where(x => x.OrderStatus == (byte)OrderStatus.WaitingForPostmanAssignment);
ViewBag.menuType = MenuType.OpenOrders;
}
if (menuType == (int)MenuType.InProgressOrders)
{
query = query.Where(x => x.PostmanId != null && x.OrderStatus != (int)OrderStatus.DeliverToCustomer
&& x.OrderStatus != (int)OrderStatus.ReturnedOrder);
ViewBag.menuType = MenuType.InProgressOrders;
}
if (!String.IsNullOrEmpty(submittedDate))
{
DateTime date = CalendarHelper.GetGregorianDateTime(submittedDate);
query = query.Where(s => s.SubmittedDate.Equals(date));
}
if (!String.IsNullOrEmpty(orderStatus))
{
var status = (byte)EnumsConverter.GetValueFromDescription<OrderStatus>(orderStatus);
query = query.Where(s => s.OrderStatus.Equals(status));
}
if (!String.IsNullOrEmpty(title))
{
query = query.Where(s => s.Title.Contains(title));
}
if (!String.IsNullOrEmpty(sellerTrackCode))
{
query = query.Where(s => s.SellerTrackCode.Contains(sellerTrackCode));
}
if (!String.IsNullOrEmpty(paymentType))
{
var type = (byte)EnumsConverter.GetValueFromDescription<PaymentType>(paymentType);
query = query.Where(s => s.PaymentType.Equals(type));
}
if (!String.IsNullOrEmpty(settlementType))
{
var settlement = (byte)EnumsConverter.GetValueFromDescription<SettlementType>(settlementType);
query = query.Where(s => s.SettlementType.Equals(settlement));
}
if (!String.IsNullOrEmpty(sellerId))
{
var selId = Convert.ToInt32(sellerId);
query = query.Where(s => s.SellerId == selId);
}
if (!String.IsNullOrEmpty(excelDocumentId))
{
var selId = Convert.ToInt32(excelDocumentId);
query = query.Where(s => s.ExcelDocumentId == selId);
}
if (!String.IsNullOrEmpty(sellerCustomerName))
{
query = query.Where(s => s.SellerCustomerName.Contains(sellerCustomerName));
}
if (!String.IsNullOrEmpty(postmanId))
{
var selId = Convert.ToInt32(postmanId);
query = query.Where(s => s.PostmanId == selId);
}
if (!String.IsNullOrEmpty(fromSubmittedDate))
{
var gregor = CalendarHelper.GetGregorianDate(fromSubmittedDate);
query = query.Where(s => s.SubmittedDate >= gregor);
}
if (!String.IsNullOrEmpty(tillSubmittedDate))
{
var gregor = CalendarHelper.GetGregorianEndDate(tillSubmittedDate);
query = query.Where(s => s.SubmittedDate <= gregor);
}
if (!String.IsNullOrEmpty(fromPickUpDate))
{
var gregor = CalendarHelper.GetGregorianDateTime(fromPickUpDate);
query = query.Where(s => s.PickUpDate >= gregor);
}
if (!String.IsNullOrEmpty(tillPickUpDate))
{
var gregor = CalendarHelper.GetGregorianDateTime(tillPickUpDate);
query = query.Where(s => s.PickUpDate <= gregor);
}
if (!String.IsNullOrEmpty(fromId))
{
var x = Convert.ToInt64(fromId);
query = query.Where(s => s.Id >= x);
}
if (!String.IsNullOrEmpty(tillId))
{
var x = Convert.ToInt64(tillId);
query = query.Where(s => s.Id <= x);
}
if (!String.IsNullOrEmpty(fromDeliveredDate))
{
var gregor = CalendarHelper.GetGregorianDate(fromDeliveredDate);
query = query.Where(s => s.DeliveredDate >= gregor);
}
if (!String.IsNullOrEmpty(tillDeliveredDate))
{
var gregor = CalendarHelper.GetGregorianEndDate(tillDeliveredDate);
query = query.Where(s => s.DeliveredDate <= gregor);
}
sortOrder = String.IsNullOrEmpty(sortOrder) ? SortKeys.PickupDateDesc : sortOrder;
switch (sortOrder)
{
case SortKeys.SubmittedDate:
query = query.OrderBy(x => x.SubmittedDate).ThenBy(x => x.Id);
break;
case SortKeys.SubmittedDateDesc:
query = query.OrderByDescending(x => x.SubmittedDate).ThenBy(x => x.Id);
break;
case SortKeys.PickupDate:
query = query.OrderBy(x => x.PickUpDate).ThenBy(x => x.Id);
break;
case SortKeys.PickupDateDesc:
query = query.OrderByDescending(x => x.PickUpDate).ThenBy(x => x.Id);
break;
case SortKeys.OrderStatus:
query = query.OrderBy(x => x.OrderStatus).ThenBy(x => x.Id);
break;
case SortKeys.OrderStatusDesc:
query = query.OrderByDescending(x => x.OrderStatus).ThenBy(x => x.Id);
break;
case SortKeys.Title:
query = query.OrderBy(x => x.Title).ThenBy(x => x.Id);
break;
case SortKeys.TitleDesc:
query = query.OrderByDescending(x => x.Title).ThenBy(x => x.Id);
break;
case SortKeys.SellerTrackCode:
query = query.OrderBy(x => x.SellerTrackCode).ThenBy(x => x.Id);
break;
case SortKeys.SellerTrackCodeDesc:
query = query.OrderByDescending(x => x.SellerTrackCode).ThenBy(x => x.Id);
break;
case SortKeys.SellerId:
query = query.OrderBy(x => x.Seller.StoreName).ThenBy(x => x.Id);
break;
case SortKeys.SellerIdDesc:
query = query.OrderByDescending(x => x.Seller.StoreName).ThenBy(x => x.Id);
break;
case SortKeys.SellerCustomerName:
query = query.OrderBy(x => x.SellerCustomerName).ThenBy(x => x.Id);
break;
case SortKeys.SellerCustomerNameDesc:
query = query.OrderByDescending(x => x.SellerCustomerName).ThenBy(x => x.Id);
break;
case SortKeys.PostmanId:
query = query.OrderBy(x => x.PostmanId).ThenBy(x => x.Id);
break;
case SortKeys.PostmanIdDesc:
query = query.OrderByDescending(x => x.PostmanId).ThenBy(x => x.Id);
break;
}
var orders = query.ToList();
var path = Path.Combine(Server.MapPath("~/App_Data/"), "orderExcel.xlsx");
if (orders.Count > 0)
{
using (var excelPackage = new ExcelPackage(new FileInfo(path)))
{
var ws = excelPackage.Workbook.Worksheets.FirstOrDefault(x => x.Name == "sheet1");
if (ws != null)
{
excelPackage.Workbook.Worksheets.Delete(ws);
}
excelPackage.Workbook.Worksheets.Add("sheet1");
var workSheet = excelPackage.Workbook.Worksheets[1];
workSheet.Cells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
workSheet.DefaultColWidth = 15;
workSheet.Column(2).Width = 20;
workSheet.Column(4).Width = 20;
workSheet.Column(5).Width = 30;
workSheet.Column(6).Width = 20;
workSheet.Column(7).Width = 20;
workSheet.Column(8).Width = 20;
workSheet.Column(11).Width = 30;
workSheet.Column(12).Width = 30;
workSheet.Column(13).Width = 40;
workSheet.Cells["I1:J1"].Merge = true;
workSheet.View.RightToLeft = true;
workSheet.Cells.Style.WrapText = true;
workSheet.Cells[1, 9].Value = "هزینه های قابل پرداخت گیرنده";
workSheet.Cells[2, 1].Value = "ردیف";
workSheet.Cells[2, 2].Value = "شماره ارجاع";
workSheet.Cells[2, 3].Value = "نوع مرسوله";
workSheet.Cells[2, 4].Value = "نام گیرنده";
workSheet.Cells[2, 5].Value = "آدرس گیرنده";
workSheet.Cells[2, 6].Value = "شماره همراه گیرنده";
workSheet.Cells[2, 7].Value = "تلفن گیرنده";
workSheet.Cells[2, 8].Value = "تاریخ دریافت سفارش";
workSheet.Cells[2, 9].Value = "وجه کالا";
workSheet.Cells[2, 10].Value = "هزینه حمل";
workSheet.Cells[2, 11].Value = "وضعیت سفارش";
workSheet.Cells[2, 12].Value = "توضیحات";
workSheet.Cells[2, 13].Value = "کد رهگیری";
for (var index = 0; index < orders.Count; index++)
{
var order = orders[index];
workSheet.Cells[index + 3, 1].Value = index + 1;
workSheet.Cells[index + 3, 2].Value = order.SellerTrackCode;
workSheet.Cells[index + 3, 3].Value = order.Title;
workSheet.Cells[index + 3, 4].Value = order.SellerCustomerName ?? string.Empty;
workSheet.Cells[index + 3, 5].Value = order.OrderAddress;
workSheet.Cells[index + 3, 6].Value = order.Mobile;
workSheet.Cells[index + 3, 7].Value = order.Tel;
workSheet.Cells[index + 3, 8].Value = order.PickUpDate.GetPersianDate();
workSheet.Cells[index + 3, 9].Value = order.OrderValue;
workSheet.Cells[index + 3, 10].Value = order.DeliveryCost;
workSheet.Cells[index + 3, 11].Value = EnumsConverter.GetDescriptionFromValue((OrderStatus)order.OrderStatus);
workSheet.Cells[index + 3, 12].Value = order.Description;
workSheet.Cells[index + 3, 13].Value = order.Id;
}
excelPackage.Save();
}
}
byte[] fileBytes = System.IO.File.ReadAllBytes(path);
string fileName = "orderExcel.xlsx";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
}
答案 0 :(得分:0)
您可能需要处理UTF-8字符:
byte[] fileBytes = Encoding.UTF8.GetBytes(File.ReadAllText(path));