public static string StatisticsSR(string parmStr) { try { StatisticsSRInfo parm = JsonConvert.DeserializeObject<StatisticsSRInfo>(parmStr); ServieRequestService ws = new ServieRequestService(); ws.Url = ConfigurationManager.AppSettings["ServiceRequestService"]; ReportDown.ServiceRequestService.MySoapHeader header = new ReportDown.ServiceRequestService.MySoapHeader(); string databaseStr = "UserId:0," + "Pass:," + "Time:" + DateTime.Now.ToString("u").Substring(0, 10) + ",Database:" + ConnManager.GetDatabase(); header.database = DES1.des(databaseStr).Replace("%2B", "+"); ws.MySoapHeaderValue = header; SRStatisticsInfo[] ds = ws.StatisticsSR(parm.statisticsType, parm.fromDate, parm.toDate, parm.groupBy, parm.orderDirection); //以上是获取数据 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = false; app.UserControl = true; app.DisplayAlerts = false;//不弹出窗口 Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks; Microsoft.Office.Interop.Excel._Workbook workbook = workbooks.Add(fileDir + "SRStatistics_" + parm.graphType + ".xlsx"); //加载模板 Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Sheets; Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1); //第一个工作薄。 if (worksheet == null) return "Error:worksheet not exists in workbook! "; //工作薄中没有工作表. //写入数据,Excel索引从1开始。 if (parm.groupBy == "year" || parm.groupBy == "quarter" || parm.groupBy == "month" || parm.groupBy == "day") { worksheet.Cells[2, 1] = "时间段"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; //Excel模板上表头和标题行占了2行,根据实际模板需要修改; worksheet.Cells[row_, 1] = ds[i].groupValue; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else { if (parm.groupBy == "province") { worksheet.Cells[2, 1] = "地区"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].provinceName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else if (parm.groupBy == "customer") { worksheet.Cells[2, 1] = "客户"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].customerName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else if (parm.groupBy == "department") { worksheet.Cells[2, 1] = "部门"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].departmentName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else if (parm.groupBy == "owner") { worksheet.Cells[2, 1] = "员工"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].ownerName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else if (parm.groupBy == "productId") { worksheet.Cells[2, 1] = "产品"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].productName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else if (parm.groupBy == "item") { worksheet.Cells[2, 1] = "配件"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].productName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else if (parm.groupBy == "faultPoint") { worksheet.Cells[2, 1] = "故障点"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].faultPointName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } else { worksheet.Cells[2, 1] = "故障类型"; worksheet.Cells[2, 2] = "请求总数"; for (int i = 0; i < ds.Length; i++) { int row_ = 3 + i; worksheet.Cells[row_, 1] = ds[i].faultTypeName; worksheet.Cells[row_, 2] = ds[i].totalCount; } } } //Microsoft.Office.Interop.Excel.Range rg = worksheet.Cells.get_Range("A3", worksheet.Cells[ds.Length + 2, 8]); //rg.Borders.LineStyle = 1; //单元格加边框。 worksheet.Columns.AutoFit(); //自动调整列宽。 string savaPath = fileDir + "服务请求统计.xlsx"; workbook.SaveAs(savaPath, Missing.Value, Missing.Value, Missing.Value, false, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); workbook.Close(); return savaPath; } catch (Exception e) { return ("Error:" + e.Message); } }
时间: 2024-11-05 21:30:41