首页 > 编程语言 >C# COM interact with Excel via Com Microsoft.Office.Interop.Excel,write content to sheet cell

C# COM interact with Excel via Com Microsoft.Office.Interop.Excel,write content to sheet cell

时间:2024-08-27 16:26:17浏览次数:4  
标签:via Office Interop Excel System content using Microsoft

1.Add Com Reference,Microsoft.Office.Interop.Excel

 

 

2.

using Microsoft.Office.Interop.Excel;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ConsoleApp56
{
    internal class Program
    {
        static Excel.Application excelApp;
        string path = System.IO.Directory.GetCurrentDirectory();
        static List<Book> booksList { get; set; }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            InitBooksList();
            ExcelDemo();
        }

        private static void InitBooksList()
        {
            booksList = new List<Book>();
            for(int i=0;i<1000;i++)
            {
                booksList.Add(new Book()
                {
                    Id=i,
                    Author=$"Author_{i}",
                    ISBN=$"ISBN_{i}",
                    Name=$"Name_{i}",
                    Summary=$"Summary_{i}",
                    Title=$"Title_{i}",
                    Topic=$"Topic_{i}"
                });
            }
        }

        private static void ExcelDemo()
        {
            excelApp=new Excel.Application();
            Workbook book = excelApp.Workbooks.Add(XlSheetType.xlWorksheet);
            if(book!=null)
            {
                var sheet = book.ActiveSheet;
                if(sheet!=null)
                {
                    if(booksList!=null && booksList.Count()>0)
                    {
                        var props = typeof(Book).GetProperties();
                        var propsCount = props.Count();
                        int rowCount = booksList.Count;
                        for(int i=0;i<rowCount;i++)
                        {
                            for(int j=0;j<propsCount;j++)
                            {
                                var strValue = props[j].GetValue(booksList[i])?.ToString();
                                sheet.Cells[i+1, j + 1] = strValue;
                            }
                        }
                    } 
                    string fileName = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), $"Excel_{DateTime.Now.ToString("yyyyMMddHHmmffff")}.xlsx");
                    book.SaveAs2(fileName);
                    Console.WriteLine(fileName);
                }
            }
            Console.WriteLine("Finished");
        }
    }


    public class Book
    {
        public int Id { get; set; }
        public string Author { get; set; }
        public string ISBN { get; set; }
        public string Name { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }
        
}

 

标签:via,Office,Interop,Excel,System,content,using,Microsoft
From: https://www.cnblogs.com/Fred1987/p/18382971

相关文章

  • VBA学习(60):补充:Excel VBA 选择输入/TreeView控件/在工作表中如何顺利使用TreeView控
    上一篇文章我们分享了一例通过TreeView控件,实现会计科目的选择输入的方法,(ExcelVBA选择输入/TreeView控件):然而,当今天我打开它准备发送给索要示例文件的小伙伴的时候,咦,这是什么鬼?再进入设计模式:TreeView1这个控件,它在啊在代码窗口查看:名称怎么变成了TreeView41?难......
  • C# split big picture into small pieces via graphics
    usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;using......
  • 4.Python操控Excel之格式
    1.设置字体斜体、加粗、颜色2.计算公式3.设置表格高度和宽度4.指定单元格合并5.取消指定单元格合并 ......
  • 关于java中Excel的导入导出
    前言提示:注意看文字的提醒:例如:提示:就这几个表别搞乱了就行其实很简单ExcelClassField------Excel标题栏工具类–不用管ExcelExport------导出配置工具类—用于对象表的配置上ExcelImport----导入配置工具类—用于对象表的配置上ExcelUtils-----用于接口调用上 一、配置pom......
  • 用友NC content SQL注入
    0x01漏洞描述:用友NCcontent接口处存在SQL注入漏洞,未经身份验证的远程攻击者除了可以利用SQL注入漏洞获取数据库中的信息(例如,管理员后台密码、站点的用户个人信息)之外,甚至在高权限的情况可向服务器中写入木马,进一步获取服务器系统权限。0x02影响版本:NC63、NC633、NC65......
  • 3.Python操控Excel之写
    1.openpyxl读取excel的格式流程2.openpyxl修改sheet标题并生成新excel保存3..create_sheet方法添加sheet,.remove方法删除sheet4.excel数据填充的四种方式5.1.更新excel内一列数据中的部分数据_代码部分5.2.更新excel内一列数据中的部分数据_执行结果......
  • (论文解读)Domain Adaptation via Prompt Learning
    摘要无监督域适应(UDA)旨在将从带有标签的源域数据中学习到的模型适应到未标注的目标域数据集。现有的UDA方法通过对齐源域和目标域特征空间来学习领域不变特征。这种对齐是通过约束实现的,例如统计差异最小化或对抗学习。然而,这些约束会导致语义特征结构的扭曲和类别判别性......
  • 高效数据整合:多个Excel表格的汇总与合并
    Excel文件很多都是应用在数据操作中,总是少不了需要将几份excel文件中的数据进行合并来进行应用。今天给大家分享两种excel工作表数据合并的方法。方法一:复制粘贴如果是少量的数据需要合并到一起,我们可以直接将数据复制过来复制成功之后,在工作表确定想要防止数据的位置,点击......
  • Java将数据导出为Excel文件
    使用ApachePOI生成基本ExcelApachePOI是一个强大的Java库,用来处理MicrosoftOffice文件。对于Excel文件(.xls和.xlsx)处理,提供有HSSF(.xls)和XSSF(.xlsx)等API。importorg.apache.poi.ss.usermodel.*;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importjavax.serv......
  • Java使用EasyExcel导出图片(原比例大小)到Excel中
    EasyExcel导出图片又开始写Excel导出的需求了,哈哈哈……目前的需求是将图表分析对的饼图和折线图,也就是一张完整的图片单独导出到Excel中为了方便客户在业务报告时,可以使用数据分析图片,从而更清晰准确地展示数据趋势因此关键点是将图片原比例尺寸大小导出,不能进行压缩原数......