Emgucv不完整图像分割试验(十六)——dataGridView1数据追加到excel

这篇博客介绍了在EmguCV图像处理项目中如何将dataGridView1中的数据追加到Excel文件。作者探讨了两种不同的方法,选择了使用Microsoft.Office.Interop.Excel库,并且提到了在已有Excel文件和新建文件时的操作区别。由于教程内容不完整,作者提醒读者自行搜索更多资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

教程不完整,不如不写。网搜了一大片东拼西凑才搞完,无奈嘞。

插入excel文件,网上两大流派:

using Microsoft.Office.Interop.Excel;

using NPOI;

本教程使用的是上者,喜欢后者的不用看下去了。

再者,插入excel又分为两种情况,已有excel文件以及需要新建excel文件。单独写了个类,小功能所以没怎么调整,只是微调,杂乱的注释和不明所以得代码请忽略。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Interop.Excel;

using Excel=Microsoft.Office.Interop.Excel;
using crl = System.Runtime.InteropServices.Marshal; //ReleaseComObject(Object O)

namespace Rice_Color_Extraction
{
    public class ExportDGVToExcel
    {
        private const int OLDOFFICEVESION = -4143;
        private const int NEWOFFICEVESION = 56;
        /// <summary>
        /// DataGridView导出Excel
        /// </summary>
        /// <param name="strCaption">Excel文件中的标题</param>
        /// <param name="myDGV">DataGridView 控件</param>
        /// <returns>0:成功;1:DataGridView中无记录;2:Excel无法启动;100:Cancel;9999:异常错误</returns>
        public int ExportExcel(string strCaption, DataGridView myDGV, SaveFileDialog saveFileDialog)
        {
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            //saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Export Excel File";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog.FileName == "")
                {
                    MessageBox.Show("请输入保存文件名!");
                    saveFileDialog.ShowDialog();
                }
                // 列索引,行索引,总列数,总行数
                int ColIndex = 0, RowIndex = 0;
                int ColCount = myDGV.ColumnCount, RowCount = myDGV.RowCount;

                if (myDGV.RowCount == 0)
                {
                    return 1;
                }

                // 创建Excel对象
                Microsoft.Office.Interop.Excel.Application xlApp = new ApplicationClass();
                if (xlApp == null)
                {
                    return 2;
                }
                try
                {
                    // 创建Excel工作薄
                    Workbook xlBook = xlApp.Workbooks.Add(true);
                    Worksheet xlSheet = (Worksheet)xlBook.Worksheets[1];
                  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值