方法一:NPOI(经测试与Revit二开不兼容)
使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写。
新建 EXCEL 表
string exportExcelPath = @"C:\Users\admin\Desktop\xxx.xlsx"; IWorkbook workbook = new XSSFWorkbook(); //用于创建 .xlsx ISheet sheet = workbook.CreateSheet("1");//获取第一个工作薄 IRow row = (IRow)sheet.CreateRow(0);//获取第一行 //设置第一行第一列值 row.CreateCell(0).SetCellValue("test");//设置第一行第一列值 //sheet.SetColumnWidth(0, 30 * 256);//设置第0列的列宽为30个字符 //导出excel FileStream fs = new FileStream(exportExcelPath, FileMode.Create, FileAccess.ReadWrite); workbook.Write(fs); fs.Close();
修改 EXCEL 表
string importExcelPath = @"C:\Users\admin\Desktop\xxx.xls"; IWorkbook workbook = null; using (FileStream fs = File.Open(importExcelPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)) { if (importExcelPath.EndsWith(".xlsx")) { workbook = new XSSFWorkbook(fs); } else { workbook = new HSSFWorkbook(fs); } } ISheet sheet = workbook.GetSheetAt(0);//获取第一个工作薄 IRow row = (IRow)sheet.GetRow(0);//获取第一行 row.CreateCell(0).SetCellValue("success");//设置第一行第一列值 using (FileStream fs = File.Create(importExcelPath)) { workbook.Write(fs); }
更新公式
// for XSSF XSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook); // for HSSF HSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
方法二: Microsoft.Office.Interop.Excel
对单元格进行操作并覆盖已有文件时会出现报错!
//创建Excel Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); xlApp.DisplayAlerts = false; xlApp.Visible = false; xlApp.ScreenUpdating = false; object miss = System.Type.Missing; Microsoft.Office.Interop.Excel.Workbook xlsWorkBook = xlApp.Workbooks.Add(miss); //处理数据过程,更多操作方法自行百度 Microsoft.Office.Interop.Excel.Worksheet sheet = xlsWorkBook.Worksheets.Item[1];//工作薄从1开始,不是0 sheet.Name = "明细表"; List<string> columnHeads = new List<string>() { "墙体类型","墙体名称","混凝土等级","竖向分布筋","水平分布筋","拉筋","箍筋","抗震等级","保护层厚度" }; // 表头 for (int i = 1; i < columnHeads.Count +1; i++) { sheet.Cells[1, i] = columnHeads[i-1]; } // 找到含参的墙 List<Element> walls = new List<Element>(); FilteredElementCollector collector = new FilteredElementCollector(Command.Doc).OfCategory(BuiltInCategory.OST_Walls). WhereElementIsNotElementType(); foreach (var element in collector.ToElements()) { if (element.GetParameters("保护层厚度")[0].AsValueString()!="" && element.GetParameters("保护层厚度")[0].AsValueString() != "0") { walls.Add(element); } } // 写入参数 for (int i = 0; i < walls.Count; i++) { sheet.Cells[i+2, 1] = walls[i].GetParameters("墙体类型")[0].AsString(); sheet.Cells[i + 2, 2] = walls[i].GetParameters("墙体名称")[0].AsString(); } //另存 xlsWorkBook.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //关闭Excel进程 ClosePro(xlApp, xlsWorkBook);
// 添加工作表 Worksheet sheet3 = xlsWorkBook.Worksheets.Add(Type.Missing, sheet2); sheet3.Name = "其他运行阶段"; sheet3.Cells[1, 1] = mainWinVM.OperationObject.Value1; sheet3.Cells[1, 2] = mainWinVM.OperationObject.Value2; sheet3.Cells[1, 3] = mainWinVM.OperationObject.Value3; sheet3.Cells[1, 4] = mainWinVM.OperationObject.Value4; sheet3.Cells[1, 5] = mainWinVM.OperationObject.Value5; sheet3.Cells[1, 6] = mainWinVM.OperationObject.Value6; sheet3.Cells[1, 7] = mainWinVM.OperationObject.Value7; sheet3.Cells[1, 8] = mainWinVM.OperationObject.Value8;
[DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); // 关闭EXCEL public void ClosePro(Microsoft.Office.Interop.Excel.Application xlApp, Microsoft.Office.Interop.Excel.Workbook xlsWorkBook) { if (xlsWorkBook != null) xlsWorkBook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); // 安全回收进程 System.GC.GetGeneration(xlApp); IntPtr t = new IntPtr(xlApp.Hwnd); //获取句柄 int k = 0; GetWindowThreadProcessId(t, out k); //获取进程唯一标志 System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); //关闭进程 }
参考文章:
NPOI - GitHub
C# NPOI
C#读写Excel的几种方法
C#实战008:Excel操作-创建新的Excel工作表_kevinfan的博客-CSDN博客
WPF之导入导出Excel_sanjiawan的专栏-CSDN博客
C# SaveFileDialog使用_风随星月-CSDN博客_c#savefiledialog用法