C#读写Excel
网上找了很多方法,都没有突出重点,着急着用,快速总结一下,把重点部分突出。
读:ws.get_Range("A1", Type.Missing);
写:ws.Cells[1, 1] = "修改的内容";
参考:http://greatverve.cnblogs.com/archive/2010/08/19/csharp-excel.html
读: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 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[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;
}
}
}
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[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;
}
}
}
以下是一些对excel的一些基本操作
1:工程对excel类库的导入,如:c:\program files\Microsoft office\offiece11\excel.exe
2:命名控件的引入: using Microsoft.office.Interop.Excel;
3:如果是对一个已经存在的excel文件进行操作则:
url:http://greatverve.cnblogs.com/archive/2012/01/31/csharp-read-write-excel.html
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);
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);
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。