using System;
using System.Web;
using EF;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq; using System.IO; using Aspose.Cells; //using Microsoft.Office.Interop.Excel; //using System.Reflection; public class ToOverTimexls : IHttpHandler { public void ProcessRequest(HttpContext context) { int oname = 0, years = 0, month = 0; if (context.Request["name"] != null) { oname = int.Parse(context.Request["name"]); } if (context.Request["years"] != null) { years = int.Parse(context.Request["years"]); } if (context.Request["month"] != null) { month = int.Parse(context.Request["month"]); } //oname = 1; years = 2016; month = 1; using (WorkRecordEntities db = new WorkRecordEntities()) { IList<OverTime> list = db.OverTime.Where(o => o.StaffID == oname && o.StartTime.Year == years && o.StartTime.Month == month).ToList(); var name = db.Staff.Where(o => o.StaffID == oname).FirstOrDefault().FullName; //建立一个Excel进程 Application // string SavaFilesPath = System.Configuration.ConfigurationManager.AppSettings["DownLoad"] + Guid.NewGuid() + ".xls"; string SavaFilesPath = context.Server.MapPath("~/Download") + "\\" + Guid.NewGuid() + ".xls"; // Application excelApplication = new Application(); // //默认值为 True。如果不想在宏运行时被无穷无尽的提示和警告消息所困扰,请将本属性设置为 False;这样每次出现需用户应答的消息时,Microsoft Excel // // 将选择默认应答。 // //如果将该属性设置为 False,则在代码运行结束后,Micorosoft Excel 将该属性设置为 True,除非正运行交叉处理代码。 // //如果使用工作簿的 SaveAs 方法覆盖现有文件,“覆盖”警告默认为“No”,当 DisplayAlerts 属性值设置为 True 时,Excel 选择“Yes”。 // excelApplication.DisplayAlerts = false; // // 建立或打开一个 Workbook对象生成新Workbook // Workbook workbook = excelApplication.Workbooks.Add(Missing.Value); // //int x = 2; // Worksheet lastWorksheet = (Worksheet)workbook.Worksheets.get_Item(workbook.Worksheets.Count); // Worksheet newSheet = (Worksheet)workbook.Worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing); // //表头 // newSheet.Cells[4, 1] = "No.(序号)"; // newSheet.Cells[4, 2] = "Date(日期)"; // newSheet.Cells[4, 3] = "Mon.~Sun."; // newSheet.Cells[4, 4] = "From(开始)"; // newSheet.Cells[4, 5] = "To(止)"; // newSheet.Cells[4, 6] = "OT Hrs.(时间)"; // newSheet.Cells[4, 7] = "Evnt(加班事由)"; // newSheet.Cells[4, 8] = "Approve(审批)"; // newSheet.Cells[3, 4] = "To(加班时间)"; // newSheet.Cells[1,4]="加班申请表"; // newSheet.Cells[2,1]="部门:后台"; newSheet.Cells[2, 2] = "后台"; // newSheet.Cells[2, 6] = "姓名:"+name; // //newSheet.Cells[2, 7] = name; // newSheet.get_Range("A1", "H1").Merge(newSheet.get_Range("A1", "H1").MergeCells); // newSheet.get_Range("A2", "D2").Merge(newSheet.get_Range("A2", "D2").MergeCells); // newSheet.get_Range("F2", "H2").Merge(newSheet.get_Range("F2", "H2").MergeCells); // newSheet.get_Range("A3", "A4").Merge(newSheet.get_Range("A3", "A4").MergeCells); // newSheet.get_Range("B3", "B4").Merge(newSheet.get_Range("B3", "B4").MergeCells); // newSheet.get_Range("C3", "C4").Merge(newSheet.get_Range("C3", "C4").MergeCells); // newSheet.get_Range("D3", "F3").Merge(newSheet.get_Range("D3", "F3").MergeCells); // newSheet.get_Range("G3", "G4").Merge(newSheet.get_Range("G3", "G4").MergeCells); // newSheet.get_Range("H3", "H4").Merge(newSheet.get_Range("H3", "H4").MergeCells); // newSheet.get_Range("A3", "H3").Interior.ColorIndex = 15; // newSheet.get_Range("A4", "H4").Interior.ColorIndex = 15; // Range range1 = newSheet.get_Range("A1", "H1"); // range1.HorizontalAlignment = XlHAlign.xlHAlignCenter; // range1.WrapText = true; // Range range = newSheet.get_Range("A3", "H4"); // range.HorizontalAlignment = XlHAlign.xlHAlignCenter; // range.Font.Size = 10; // range.Borders.LineStyle = 1; // //设置边框 range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlMedium, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb()); // range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlMedium; // range.WrapText = true; var x = 3; // var x = 5; // for (var i = 0; i < list.Count; i++) // { // newSheet.Cells[x + i, 1] = i + 1; // newSheet.Cells[x + i, 2] = list[i].StartTime.Month + "月" + list[i].StartTime.Day + "日"; // newSheet.Cells[x + i, 3] = GetWeekCHA((list[i].StartTime.DayOfWeek).ToString()); // newSheet.Cells[x + i, 4] = list[i].StartTime.ToString("yyyy/MM/dd HH:mm:ss"); // newSheet.Cells[x + i, 5] = list[i].EndTime.ToString("yyyy/MM/dd HH:mm:ss"); // int ts = (list[i].EndTime - list[i].StartTime).Hours; // newSheet.Cells[x + i, 6] = ts; // newSheet.Cells[x + i, 7] = list[i].Description; // newSheet.Cells[x + i, 8] = ""; // } // newSheet.Cells.Columns.AutoFit(); // //删除原来的空Sheet // ((Worksheet)workbook.Worksheets.get_Item(1)).Delete(); // ((Worksheet)workbook.Worksheets.get_Item(1)).Delete(); // ((Worksheet)workbook.Worksheets.get_Item(1)).Delete(); // //设置默认选中是第一个Sheet 类似于Select(); // ((Worksheet)workbook.Worksheets.get_Item(1)).Activate(); // try // { // workbook.Close(true, SavaFilesPath, Missing.Value); // } // catch (Exception e) // { // throw e; // } // UploadExcel(SavaFilesPath, true); // excelApplication.Quit(); Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets[0]; Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中 worksheet.PageSetup.Orientation = PageOrientationType.Landscape;//横向打印 worksheet.PageSetup.Zoom = 100;//以100%的缩放模式打开 worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4; Range range; Cell cell; range = worksheet.Cells.CreateRange(0, 0, 1, 8); range.Merge(); range.RowHeight = 20; range.ColumnWidth = 15; cell = range[0, 0]; cell.PutValue("加班申请表"); cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(1, 0, 1, 2); range.Merge(); range.RowHeight = 15; cell = range[0, 0]; cell.PutValue("部门:后台"); range = worksheet.Cells.CreateRange(1, 4, 1, 2); range.Merge(); range.RowHeight = 15; cell = range[0, 0]; cell.PutValue("姓名:" + name); //range = worksheet.Cells.CreateRange(1, 5, 1, 1); //range.Merge(); //range.RowHeight = 15; //cell = range[0, 0]; //cell.PutValue("方亭"); range = worksheet.Cells.CreateRange(2, 0, 2, 1); range.Merge(); cell = range[0, 0]; cell.PutValue("No.(序号)"); cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 1, 2, 1); range.Merge(); cell = range[0, 0]; cell.PutValue("Date(日期)"); range = worksheet.Cells.CreateRange(2, 2, 2, 1); range.Merge(); cell = range[0, 0]; cell.PutValue("Mon.~Sun."); cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 3, 1, 3); range.Merge(); range.ColumnWidth = 20; cell = range[0, 0]; cell.PutValue("To(加班时间)"); cell.SetStyle(styleTitle); cell = worksheet.Cells[3, 3]; cell.PutValue("From(开始)"); cell.SetStyle(styleTitle); cell = worksheet.Cells[3, 4]; cell.PutValue("To(止)"); cell.SetStyle(styleTitle); cell = worksheet.Cells[3, 5]; cell.PutValue("OT Hrs.(时间)"); cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 6, 2, 1); range.Merge(); cell = range[0, 0]; cell.PutValue("Evnt(加班事由)"); cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 7, 2, 1); range.Merge(); cell = range[0, 0]; cell.PutValue("Approve(审批)"); cell.SetStyle(styleTitle); for (var i = 0; i < list.Count; i++) { //newSheet.Cells[x + i, 1] = i + 1; //newSheet.Cells[x + i, 2] = list[i].StartTime.Month + "月" + list[i].StartTime.Day + "日"; //newSheet.Cells[x + i, 3] = GetWeekCHA((list[i].StartTime.DayOfWeek).ToString()); //newSheet.Cells[x + i, 4] = list[i].StartTime.ToString("yyyy/MM/dd HH:mm:ss"); //newSheet.Cells[x + i, 5] = list[i].EndTime.ToString("yyyy/MM/dd HH:mm:ss"); //int ts = (list[i].EndTime - list[i].StartTime).Hours; //newSheet.Cells[x + i, 6] = ts; //newSheet.Cells[x + i, 7] = list[i].Description; //newSheet.Cells[x + i, 8] = ""; cell = worksheet.Cells[4 + i, 0]; cell.PutValue(i + 1); cell.SetStyle(styleTitle); cell = worksheet.Cells[4 + i, 1]; cell.PutValue(list[i].StartTime.Month + "月" + list[i].StartTime.Day + "日"); cell.SetStyle(styleTitle); cell = worksheet.Cells[4 + i, 2]; cell.PutValue(GetWeekCHA((list[i].StartTime.DayOfWeek).ToString())); cell.SetStyle(styleTitle); cell = worksheet.Cells[4 + i, 3]; cell.PutValue(list[i].StartTime.ToString("yyyy/MM/dd HH:mm:ss")); cell.SetStyle(styleTitle); cell = worksheet.Cells[4 + i, 4]; cell.PutValue(list[i].EndTime.ToString("yyyy/MM/dd HH:mm:ss")); cell.SetStyle(styleTitle); int ts = (list[i].EndTime - list[i].StartTime).Hours; cell = worksheet.Cells[4 + i, 5]; cell.PutValue(ts); cell.SetStyle(styleTitle); cell = worksheet.Cells[4 + i, 6]; cell.PutValue(list[i].Description); cell.SetStyle(styleTitle); cell = worksheet.Cells[4 + i, 7]; cell.PutValue(""); cell.SetStyle(styleTitle); } workbook.Save(SavaFilesPath); UploadExcel(SavaFilesPath, true); } } /// <summary> /// 返回星期中文名 /// </summary> /// <param name="WeekENG">星期英文名</param> /// <returns></returns> public string GetWeekCHA(string WeekENG) { string return_value = ""; switch (WeekENG) { case "Monday": return_value = "星期一"; return return_value; case "Tuesday": return_value = "星期二"; return return_value; case "Wednesday": return_value = "星期三"; return return_value; case "Thursday": return_value = "星期四"; return return_value; case "Friday": return_value = "星期五"; return return_value; case "Saturday": return_value = "星期六"; return return_value; case "Sunday": return_value = "星期日"; return return_value; } return return_value; } /// <summary> /// 提供下载 /// </summary> ///