zoukankan      html  css  js  c++  java
  • C#读写Excel(转)

    网上找了很多方法,都没有突出重点,着急着用,快速总结一下,把重点部分突出。
    读:ws.get_Range("A1", Type.Missing);
    写:ws.Cells[1, 1] = "修改的内容";

    复制代码
    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 FilesDataSpaceKeyword.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[1, 1] = "修改的内容";
                //保存Excel
                wb.Save();
                wb.Close(null, null, null);
                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 filesMicrosoft officeoffiece11excel.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

  • 相关阅读:
    linux centos&Ubuntu&RedHat更换软件源
    linux及windows安装maven
    No converter found for return value of type: class com.alibaba.fastjson.JSON解决办法
    linux sudo命令失败 提示sudo:/usr/bin/sudo 必须属于用户 ID 0(的用户)并且设置 setuid 位
    Linux常用命令
    Linux安装Java+Eclipse或IDEA
    Python基础编程:字符编码、数据类型、列表
    linux系统挂载u盘拷贝文件
    linux(服务器)如何确认网卡(网口)对应的配置文件
    Python第三方库requests的编码问题
  • 原文地址:https://www.cnblogs.com/dylanblog/p/4202982.html
Copyright © 2011-2022 走看看