C# 创建Excel并写入内容

        1 增加应用      Microsoft.Office.Interop.Excel
        2 引用命名空间  using Excel = Microsoft.Office.Interop.Excel;
        /// <summary>
        /// If the supplied excel File does not exist then Create it
        /// </summary>
        /// <param name="FileName"></param>
        private void CreateExcelFile(string FileName)
        {
            //create
            object Nothing = System.Reflection.Missing.Value;
            var app = new Excel.Application();
            app.Visible = false;
            Excel.Workbook workBook = app.Workbooks.Add(Nothing);
            Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];
            worksheet.Name = "Work";
            //headline
            worksheet.Cells[1, 1] = "FileName";
            worksheet.Cells[1, 2] = "FindString";
            worksheet.Cells[1, 3] = "ReplaceString";

            worksheet.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
            workBook.Close(false, Type.Missing, Type.Missing);
            app.Quit();
        }

        /// <summary>
        /// open an excel file,then write the content to file
        /// </summary>
        /// <param name="FileName">file name</param>
        /// <param name="findString">first cloumn</param>
        /// <param name="replaceString">second cloumn</param>
        private void WriteToExcel(string excelName,string filename,string findString,string replaceString)
        {
            //open
            object Nothing = System.Reflection.Missing.Value;
            var app = new Excel.Application();
            app.Visible = false;
            Excel.Workbook mybook = app.Workbooks.Open(excelName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
            Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets[1];
            mysheet.Activate();     
            //get activate sheet max row count
            int maxrow = mysheet.UsedRange.Rows.Count + 1;
            mysheet.Cells[maxrow, 1] = filename;
            mysheet.Cells[maxrow, 2] = findString;
            mysheet.Cells[maxrow, 3] = replaceString;
            mybook.Save();
            mybook.Close(false, Type.Missing, Type.Missing);
            mybook = null;
            //quit excel app
            app.Quit();
        }

  • 11
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#中的NPOI库来创建写入Excel文件。下面是一个简单的示例代码,演示了如何使用NPOI库创建一个Excel文件,并将数据写入到文件中: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; using System.IO; public void ExportToExcel(string filePath) { // 创建一个Workbook对象,表示整个Excel文件 IWorkbook workbook = new HSSFWorkbook(); // 创建一个Sheet对象,表示Excel中的一个Sheet ISheet sheet = workbook.CreateSheet("Sheet1"); // 创建第一行,并写入表头数据 IRow headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("姓名"); headerRow.CreateCell(1).SetCellValue("年龄"); headerRow.CreateCell(2).SetCellValue("性别"); // 写入数据行 for (int i = 0; i < data.Count; i++) { IRow dataRow = sheet.CreateRow(i + 1); dataRow.CreateCell(0).SetCellValue(data[i].Name); dataRow.CreateCell(1).SetCellValue(data[i].Age); dataRow.CreateCell(2).SetCellValue(data[i].Gender); } // 调整列宽 sheet.AutoSizeColumn(0); sheet.AutoSizeColumn(1); sheet.AutoSizeColumn(2); // 将Workbook保存为Excel文件 using (FileStream fs = new FileStream(filePath, FileMode.Create)) { workbook.Write(fs); } } ``` 以上代码创建了一个名为"Sheet1"的工作表,并在第一行写入了表头数据。然后,使用循环将数据写入数据行中。最后,将Workbook保存为Excel文件。 在使用该代码之前,你需要在项目中引用NPOI库,并在代码中引入相应的命名空间。此外,你可能需要根据自己的需求修改代码以适应你的数据结构和文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值