Visual Studio 2005 C# 读写Excel文件

本文介绍如何使用Visual Studio 2005通过添加Microsoft Office 12.0 Object Library引用,实现对Excel 2007文件的操作,包括读写数据及调整列宽等。

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

做作业的时候查了一点儿资料,
用的vs2k5 读 excel 2007
发现用起来非常简单。。。现在编程语言没话说!

1
项目-添加引用-COM-Microsoft Excel 12.0 Object Library
&& -Microsoft Office 12.0 Object Library

2
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;

3
            string originalFile = System.Windows.Forms.Application.StartupPath + @".\a.xlsx";
            string outputFile;
            SaveFileDialog save = new SaveFileDialog();
            save.InitialDirectory = "D:\\";
            save.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
            save.FilterIndex = 1;
            save.RestoreDirectory = true;
            if (save.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    outputFile = save.FileName;
                    System.IO.File.Copy(originalFile, outputFile, true);
                    ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    //打开目标文件outputFile
                    RSbook = ExcelRS.Workbooks.Open(outputFile, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing, missing, missing, missing);
                    //设置第一个工作溥
                    RSsheet = (Microsoft.Office.Interop.Excel.Worksheet)RSbook.Sheets.get_Item(1);
                    //激活当前工作溥
                    RSsheet.Activate();
                    RSsheet.Cells[1, 1] = dataGridView1.SelectedRows.Count;
                    for (int i = 0; i < dataGridView1.SelectedRows.Count; ++i)
                    {
                        for (int j = 0; j < 11; ++j)
                        {
                            RSsheet.Cells[i + 2, j + 1] = dataGridView1.Rows[dataGridView1.SelectedRows[i].Index].Cells[j].Value.ToString().Trim();
                            // RSsheet.Cells
                            RSsheet.get_Range(RSsheet.Cells[i + 2, j + 1], RSsheet.Cells[i + 2, j + 1]).EntireColumn.ColumnWidth = 40;
                            //RSsheet.get_Range(RSsheet.Cells[i + 2, j + 1], missing).auto
                        }
                    }
                    RSbook.Save();
                    ExcelRS.DisplayAlerts = true;
                    ExcelRS.Visible = true;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    ExcelRS.Quit();
                }
            }



4
            OpenFileDialog open = new OpenFileDialog();
            open.InitialDirectory = @"D:\";
            open.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
            open.FilterIndex = 1;
            open.RestoreDirectory = true;

            if (open.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    RSbook = ExcelRS.Workbooks.Open(open.FileName, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing, missing, missing, missing);
                    RSsheet = (Microsoft.Office.Interop.Excel.Worksheet)RSbook.Sheets.get_Item(1);
                    RSsheet.Activate();
                    Microsoft.Office.Interop.Excel.Range range = RSsheet.get_Range("A" + i, Type.Missing);
                    counts = int.Parse(range.Text.ToString().Trim());
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    ExcelRS.Quit();
                    conn.Close();
                }
            }

转载于:https://www.cnblogs.com/bluewelkin/archive/2008/08/25/1275642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值