C# 读写Excel
using System.Reflection;
using Excel=Microsoft.Office.Interop;
protected void Button1_Click(object sender, EventArgs e)
{
string rootPath = Server.MapPath("\\");
string lastPath = "ExportExcel\\CData.xls";
string templatePath = "JiaBanDaoXiu.xlt";
Excel.Excel.XlFileFormat version = Excel.Excel.XlFileFormat.xlExcel8;//Excel 2003版本
//if (System.IO.Path.GetExtension(fileName).ToLower() == ".xlsx") version = Excel.Excel.XlFileFormat.xlOpenXMLWorkbook;//Excel 2007版本
try
{
//创建Application对象
Excel.Excel.Application xApp = new Excel.Excel.Application();
xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
if (File.Exists(rootPath + templatePath))
{
Label1.Text = "找到模板";
}
else
{
Label1.Text = "找不到模板文件!";
}
Excel.Excel.Workbook xBook = xApp.Workbooks._Open(rootPath + templatePath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//指定要操作的Sheet,两种方式:
Excel.Excel.Worksheet xSheet = (Excel.Excel.Worksheet)xBook.Sheets[1];
//写入数据
Excel.Excel.Range rng3 = xSheet.get_Range("C6", Missing.Value);
rng3.Value2 = "Test";
rng3.Interior.ColorIndex = 6; //设置Range的背景色
// 保存方式二:保存WorkSheet
if (File.Exists(rootPath + lastPath))
File.Delete(rootPath + lastPath);
xSheet.SaveAs(rootPath + lastPath,
version , Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//string fileName =FilePath+"CData2.xls";
//Excel.XlFileFormat version = Excel.XlFileFormat.xlExcel8;//Excel 2003版本
//if (System.IO.Path.GetExtension(fileName).ToLower() == ".xlsx") version = Excel.XlFileFormat.xlOpenXMLWorkbook;//Excel 2007版本
//xSheet.SaveAs(fileName, version, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
rng3 = null;
xSheet = null;
xBook = null;
xApp.Workbooks.Close();
xApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出
xApp = null;
//this.Load += new System.EventHandler(this.Page_Load);
}
catch (System.Exception ex)
{
Label1.Text = "ERROR";
Response.Write(ex.Message + "ERROR1");
}
}
===========================================================================================
将Excel操作总结如下:
//创建Application对象
Excel.Excel.XlFileFormat version = Excel.Excel.XlFileFormat.xlExcel8;//Excel 2003版本
//if (System.IO.Path.GetExtension(fileName).ToLower() == ".xlsx") version = Excel.Excel.XlFileFormat.xlOpenXMLWorkbook;//Excel 2007版本
Excel.Excel.Application xApp = new Excel.Excel.Application();
xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Excel.Excel.Workbook xBook = xApp.Workbooks._Open("D:\\JiaBanDaoXiu.xlt",
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码
//指定要操作的Sheet,两种方式:
Excel.Excel.Worksheet xSheet = (Excel.Excel.Worksheet)xBook.Sheets[1];
//Excel.Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet;
////读取数据,通过Range对象
//Excel.Range rng1 = xSheet.get_Range("A1", Type.Missing);
//Console.WriteLine(rng1.Value2);
////读取,通过Range对象,但使用不同的接口得到Range
//Excel.Range rng2 = (Excel.Range)xSheet.Cells[3, 1];
//Console.WriteLine(rng2.Value2);
//写入数据
Excel.Excel.Range rng3 = xSheet.get_Range("C6", Missing.Value);
rng3.Value2 = Label1.Text;
rng3.Interior.ColorIndex = 6; //设置Range的背景色
//保存方式一:保存WorkBook
if (File.Exists("D:\\CData.xls"))
File.Delete("D:\\CData.xls");
xBook.SaveAs(@"D:\CData.xls",Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
////保存方式二:保存WorkSheet
//xSheet.SaveAs(@"D:\CData12.xls",
//Missing.Value, Missing.Value, Missing.Value, Missing.Value,
//Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
////保存方式三
xBook.Save();
xSheet = null;
xBook = null;
xApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出
xApp = null;