username@email.com
2025-05-12 ae6e40362a745caef9ead36f81f38313fb8c2c66
CY_ECommercePlatform/CY.WebForm/Pages/business/DeliverPlanDakaList.aspx.cs
@@ -18,6 +18,9 @@
using CY.SQLDAL;
using System.Data.SqlClient;
using CY.Infrastructure.Logging;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
namespace CY.WebForm.Pages.business
{
@@ -228,9 +231,103 @@
            UCPager1.AspNetPager.RecordCount = pa.RecordCount;
        }
        //查询事件
        protected void btn_Daochu_Click(object src, EventArgs e)
        {
            byte[] fileBytes = ExportToExcel();
            if (fileBytes != null)
            {
                Response.Clear();
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AppendHeader("Content-Disposition", "attachment; filename=打卡导出.xlsx");
                Response.BinaryWrite(fileBytes);
                Response.End();
            }
            else
            {
                // 处理导出失败的情况
                Response.Write("导出失败,请稍后重试!");
            }
        }
        public byte[] ExportToExcel()
        {
            byte[] bt = null;
            try
            {
                Pagination pa = new Pagination();
                pa.PageSize = 1000;
                pa.PageIndex = 1;
                IEnumerable<OA_DriverRecord> results = _oA_DriverRecordBLL.SelectModelPage(pa,
                this.txtCreatTimestart.Value,
                 this.txtCreatTimeend.Value,
                 this.selCarId.Value,
                this.txtCreater.Value,
                this.selClockType.Value
              );
                // 创建工作簿
                IWorkbook workbook = new XSSFWorkbook();
                ISheet sheet = workbook.CreateSheet("导出数据");
                // 创建表头
                IRow headerRow = sheet.CreateRow(0);
                headerRow.CreateCell(0).SetCellValue("编号");
                headerRow.CreateCell(1).SetCellValue("车辆");
                headerRow.CreateCell(2).SetCellValue("驾驶员");
                headerRow.CreateCell(3).SetCellValue("打卡时间");
                headerRow.CreateCell(4).SetCellValue("打卡类型");
                headerRow.CreateCell(5).SetCellValue("未装货情况");
                headerRow.CreateCell(6).SetCellValue("车况");
                headerRow.CreateCell(7).SetCellValue("备注");
                headerRow.CreateCell(8).SetCellValue("里程(公里)");
                // 添加数据
                int rowIndex = 1;
                foreach (var oA_DriverRecord in results)
                {
                    IRow row = sheet.CreateRow(rowIndex++);
                    row.CreateCell(0).SetCellValue((rowIndex - 1).ToString());
                    row.CreateCell(1).SetCellValue(oA_DriverRecord.CarName);
                    row.CreateCell(2).SetCellValue(oA_DriverRecord.Creater);
                    row.CreateCell(3).SetCellValue(oA_DriverRecord.CreatTime.ToString("yyyy-MM-dd"));
                    row.CreateCell(4).SetCellValue(oA_DriverRecord.ClockTypeName);
                    row.CreateCell(5).SetCellValue(oA_DriverRecord.EmptyCause);
                    row.CreateCell(6).SetCellValue(oA_DriverRecord.CarCondition);
                    row.CreateCell(7).SetCellValue(oA_DriverRecord.Remark);
                    row.CreateCell(8).SetCellValue(oA_DriverRecord.Mileage.ToString());
                }
                // 将工作簿写入内存流
                using (var ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    bt = ms.ToArray();
                }
            }
            catch (Exception e)
            {
                bt = null;
                // 记录日志
            }
            return bt;
        }
    }
}