C#读取Excel的方法

39 篇文章 0 订阅
14 篇文章 0 订阅

 

C#读写Excel文件并打印输出的Demo

 

1、 创建一个叫DemoExcel的项目

2、 引用COM,包括:Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library

建议安装正版OFFICE,而且版本在11.0以上(Office2003以上),引用以上两个Com后,在项目引用栏发现多了Excel、Microsoft.Office.Core,VBIDE三个 Library.

 

 

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.IO;
using System.Reflection;

namespace DemoExcel
{
    public partial class Form1 : Form
    {
        private  object missing = Missing.Value;
        private Microsoft.Office.Interop.Excel.Application ExcelRS;
        private Microsoft.Office.Interop.Excel.Workbook RSbook;
        private Microsoft.Office.Interop.Excel.Worksheet RSsheet;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dataSet1.STREET”中。您可以根据需要移动或移除它。
            this.sTREETTableAdapter.Fill(this.dataSet1.STREET);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string OutFilePath = System.Windows.Forms.Application.StartupPath + @" emp.xls";
           
            string TemplateFilePath = System.Windows.Forms.Application.StartupPath + @"模版.xls";
            PrintInit(TemplateFilePath,OutFilePath);
        }
        Excle输出前初始化#region Excle输出前初始化
        /** <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool PrintInit(string templetFile, string outputFile)
        {
            try
            {
                if (templetFile == null)
                {
                    MessageBox.Show("Excel模板文件路径不能为空!");
                    return false;
                }
                if (outputFile == null)
                {
                    MessageBox.Show("输出Excel文件路径不能为空!");
                    return false;
                }
                //把模版文件templetFile拷贝到目输出文件outputFile中,并且目标文件可以改写
                System.IO.File.Copy(templetFile, outputFile, true);
                if (this.ExcelRS != null)
                    ExcelRS = null;
                //实例化ExcelRS对象
                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();
                
                在当前工作溥写入内容#region 在当前工作溥写入内容
                for (int i = 0; i < this.dataGridView1.RowCount; i++)
                {
                    RSsheet.Cells[3 + i, 1] = this.dataGridView1[0, i].Value.ToString();
                    RSsheet.Cells[3 + i, 2] = this.dataGridView1[1, i].Value.ToString();
                    RSsheet.Cells[3 + i, 3] = this.dataGridView1[2, i].Value.ToString();
                } 
                #endregion

                //保存目标文件
                RSbook.Save();
                //设置DisplayAlerts
                ExcelRS.DisplayAlerts = false;
                ExcelRS.Visible = true;
                //ExcelRS.DisplayAlerts = true;

                //释放对象
                RSsheet = null;
                RSbook = null;
                ExcelRS = null;
                //释放内存
                GcCollect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }
        #endregion
        public void GcCollect()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}


 

文章转载自:       C#如何读取Excel             http://www.studyofnet.com/news/259.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值