现在的位置: 首页 > 综合 > 正文

C#读写Excel

2013年07月10日 ⁄ 综合 ⁄ 共 3303字 ⁄ 字号 评论关闭
网上找了很多方法,都没有突出重点,着急着用,快速总结一下,把重点部分突出。
读:ws.get_Range("A1", Type.Missing);
写:
ws.Cells[11] = "修改的内容";

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.IO;
using System.Reflection;
using MSExcel = Microsoft.Office.Interop.Excel;

namespace ExcelTool
{
    public partial class FrmMain : Form
    {
        string str = @"C:\Program Files\Data\SpaceKeyword.xlsx";
        object missing = Missing.Value;
        MSExcel.Application app = null;
        MSExcel.Workbook wb = null;
        MSExcel.Worksheet ws = null;
        MSExcel.Range r = null;
        /// <summary>
        
/// 加载Excel
        
/// </summary>
        private void InitExcel()
        { 
            //打开excel
            app = new Microsoft.Office.Interop.Excel.Application();
            wb = app.Workbooks.Open(str, false, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            app.Visible = false;//读Excel不显示出来影响用户体验

            //得到WorkSheet对象
            ws = (MSExcel.Worksheet)wb.Worksheets.get_Item(1);
        }
        /// <summary>
        
/// 读取Excel中的内容
        
/// </summary>
        private void ReadExcel()
        {
            InitExcel();
            //读取A1单元格内容
            r = ws.get_Range("A1", Type.Missing);
            string strA1 = r.Value;
            app.Quit();//退出
            MessageBox.Show(strA1);
        }
        /// <summary>
        
/// 写入Excel(Win7下要以管理员身份运行才能修改)
        
/// </summary>
        private void WriteExcel()
        {
            InitExcel();
            ws.Cells[11] = "修改的内容";
            //保存Excel
            wb.Save();
            wb.Close(nullnullnull);
            app.Workbooks.Close();
            app.Application.Quit();
            app.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

            ws = null;
            wb = null;
            app = null;
        }
    }
}

参考:http://greatverve.cnblogs.com/archive/2010/08/19/csharp-excel.html

以下是一些对excel的一些基本操作

1:工程对excel类库的导入,如:c:\program files\Microsoft office\offiece11\excel.exe

2:命名控件的引入: using Microsoft.office.Interop.Excel;

3:如果是对一个已经存在的excel文件进行操作则:
Application app=new Application();
Workbook wbook=app.Workbooks.Open("c:\\temp.xls",Type.Missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing);

Worksheet worksheet=(Worksheet)wbook.Worksheets[1];

4:如果是新建一个excel文件:
Application app=new Application();
Workbook wbook=app.Workbook.Add(Type.missing);
Worksheet worksheet=(Worksheet)wbook.Worksheets[1];

5:设置某个单元格里的内容:
worksheet.Cells[1,2]="列内容"

6读取某个单元格里的内容
string temp=((Range)worksheet.Cells[1,2]).Text;

7设置某个单元格里的格式
Excel.Range rtemp=worksheet.get_Range("A1","A1");
rtemp.Font.Name="宋体";
rtemp.Font.FontStyle="加粗";
rtemp.Font.Size=5;

8 保存新建的内容:
worksheet.SaveAs("c:\\temp.xls",Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing);

url:http://greatverve.cnblogs.com/archive/2012/01/31/csharp-read-write-excel.html

【上篇】
【下篇】

抱歉!评论已关闭.