• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VisualStudio2005C#读写Excel文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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();
                }
            }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#自动升级发布时间:2022-07-13
下一篇:
基于Asp.netC#实现HTML转图片(网页快照)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap