using System;using System.IO;using Excel = Microsoft.Office.Interop.Excel;namespace ExcelDemo{class Program{static void Main(string[] args){Excel.Application excelApp = new Excel.Application();Excel.Workbook workbook = excelApp.Workbooks.Add();Excel.Worksheet worksheet = workbook.ActiveSheet;worksheet.Cells[1, 1] = "姓名";worksheet.Cells[1, 2] = "年龄";worksheet.Cells[2, 1] = "张三";worksheet.Cells[2, 2] = "20";worksheet.Cells[3, 1] = "李四";worksheet.Cells[3, 2] = "25";string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "sample.xlsx");workbook.SaveAs(filePath);workbook.Close();excelApp.Quit();Console.WriteLine("Excel文件已保存到桌面上的sample.xlsx");Console.ReadLine();}}}

C# 操作 Excel 代码示例:写入数据并保存

原文地址: https://www.cveoy.top/t/topic/pYUD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录