private void executeToExcel(Record record) { string path = Application.StartupPath + ConfigurationManager.AppSettings["excelPath"].ToString(); File.Copy(path, Application.StartupPath + @"\test.xlsx", true); ExcelEdit excelOperator = new ExcelEdit(); excelOperator.Open(Application.StartupPath + @"\test.xlsx"); Microsoft.Office.Interop.Excel.Worksheet sheet1 = excelOperator.GetSheet("Sheet1"); sheet1.Cells[4, 5].Value = record.Operator; sheet1.Cells[4, 8].Value = record.CreateTime.ToString("yyyy-MM-dd"); sheet1.Cells[6, 2].Value = record.SeriesNum; sheet1.Cells[6, 5].Value = record.ProName; sheet1.Cells[6, 8].Value = record.Result == 1 ? "合格" : "不合格"; for (int i = 0; i < record.RecordDetailList.Count; i++) { sheet1.Cells[i + 11, 2].Value = record.RecordDetailList[i].Index; sheet1.Cells[i + 11, 3].Value = record.RecordDetailList[i].TestValue + " N·m"; sheet1.Cells[i + 11, 4].Value = record.RecordDetailList[i].Standard + " N·m"; sheet1.Cells[i + 11, 5].Value = record.RecordDetailList[i].Difference; sheet1.Cells[i + 11, 6].Value = record.RecordDetailList[i].Percent + "%"; sheet1.Cells[i + 11, 7].Value = record.Result == 1 ? "合格" : "不合格"; sheet1.Cells[i + 11, 8].Value = record.RecordDetailList[i].Upper + " N·m"; sheet1.Cells[i + 11, 9].Value = record.RecordDetailList[i].Lower + " N·m"; } if (ConfigurationManager.AppSettings["printMode"].ToString().Equals("preview")) { sheet1.PrintPreview(); } else if (ConfigurationManager.AppSettings["printMode"].ToString().Equals("print")) { sheet1.PrintOutEx(); } excelOperator.Save(); excelOperator.Close(); GC.Collect(); }
/// <summary> /// 保存数据到文件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sourceData"></param> /// <param name="fileName"></param> /// <param name="sheetName"></param> public static void saveDataToFile <T>(List <T> sourceData) { Dictionary <string, string> fileinfo = getFileInfo <T>(); string fileName = fileinfo["ExcelName"]; string sheetName = fileinfo["SheetName"]; ExcelEdit excel = new ExcelEdit(); string str = System.Windows.Forms.Application.StartupPath; Worksheet ws = null; if (!Directory.Exists(str)) { Directory.CreateDirectory(str); } if (!File.Exists(str + @"\" + fileName)) { excel.mFilename = str + @"\" + fileName; excel.Create(); excel.AddSheet(sheetName); } else { excel.Open(str + @"\" + fileName); } if (excel.GetSheet(sheetName) == null) { ws = excel.AddSheet(sheetName); } else { ws = excel.GetSheet(sheetName); } int y = 2; foreach (T item in sourceData) { int x = 1; foreach (PropertyInfo obj in item.GetType().GetProperties()) { object value = null; if (y == 2) { value = obj.Name.ToString(); excel.SetCellValue(ws, 1, x, value); } value = item.GetType().GetProperty(obj.Name.ToString()).GetValue(item); Range titleRange = ws.Range[ws.Cells[y, x], ws.Cells[y, x]]; //选中标题 titleRange.HorizontalAlignment = XlHAlign.xlHAlignCenter; //水平居中 if (titleRange.Value != null && value != null && value.Equals(titleRange.Value)) { x++; continue; } else { excel.SetCellValue(ws, y, x, value); x++; } } y++; } excel.wb.RefreshAll(); if (File.Exists(str + @"\" + fileName)) { excel.Save(); } else { excel.SaveAs(str + @"\" + fileName); } excel.Close(); }