C# 创建Excel并写入内容
2015-08-18 13:34
531 查看
1 增加应用 Microsoft.Office.Interop.Excel
2 引用命名空间 using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
/// If the supplied excel File does not exist then Create it
/// </summary>
/// <param name="FileName"></param>
private void CreateExcelFile(string FileName)
{
//create
object Nothing = System.Reflection.Missing.Value;
var app = new Excel.Application();
app.Visible = false;
Excel.Workbook workBook = app.Workbooks.Add(Nothing);
Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];
worksheet.Name = "Work";
//headline
worksheet.Cells[1, 1] = "FileName";
worksheet.Cells[1, 2] = "FindString";
worksheet.Cells[1, 3] = "ReplaceString";
worksheet.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
workBook.Close(false, Type.Missing, Type.Missing);
app.Quit();
}
/// <summary>
/// open an excel file,then write the content to file
/// </summary>
/// <param name="FileName">file name</param>
/// <param name="findString">first cloumn</param>
/// <param name="replaceString">second cloumn</param>
private void WriteToExcel(string excelName,string filename,string findString,string replaceString)
{
//open
object Nothing = System.Reflection.Missing.Value;
var app = new Excel.Application();
app.Visible = false;
Excel.Workbook mybook = app.Workbooks.Open(excelName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets[1];
mysheet.Activate();
//get activate sheet max row count
int maxrow = mysheet.UsedRange.Rows.Count + 1;
mysheet.Cells[maxrow, 1] = filename;
mysheet.Cells[maxrow, 2] = findString;
mysheet.Cells[maxrow, 3] = replaceString;
mybook.Save();
mybook.Close(false, Type.Missing, Type.Missing);
mybook = null;
//quit excel app
app.Quit();
}
2 引用命名空间 using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
/// If the supplied excel File does not exist then Create it
/// </summary>
/// <param name="FileName"></param>
private void CreateExcelFile(string FileName)
{
//create
object Nothing = System.Reflection.Missing.Value;
var app = new Excel.Application();
app.Visible = false;
Excel.Workbook workBook = app.Workbooks.Add(Nothing);
Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];
worksheet.Name = "Work";
//headline
worksheet.Cells[1, 1] = "FileName";
worksheet.Cells[1, 2] = "FindString";
worksheet.Cells[1, 3] = "ReplaceString";
worksheet.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
workBook.Close(false, Type.Missing, Type.Missing);
app.Quit();
}
/// <summary>
/// open an excel file,then write the content to file
/// </summary>
/// <param name="FileName">file name</param>
/// <param name="findString">first cloumn</param>
/// <param name="replaceString">second cloumn</param>
private void WriteToExcel(string excelName,string filename,string findString,string replaceString)
{
//open
object Nothing = System.Reflection.Missing.Value;
var app = new Excel.Application();
app.Visible = false;
Excel.Workbook mybook = app.Workbooks.Open(excelName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets[1];
mysheet.Activate();
//get activate sheet max row count
int maxrow = mysheet.UsedRange.Rows.Count + 1;
mysheet.Cells[maxrow, 1] = filename;
mysheet.Cells[maxrow, 2] = findString;
mysheet.Cells[maxrow, 3] = replaceString;
mybook.Save();
mybook.Close(false, Type.Missing, Type.Missing);
mybook = null;
//quit excel app
app.Quit();
}
相关文章推荐
- 编写高质量代码改善C#程序的157个建议——建议70:避免在调用栈较低的位置记录异常
- C#实现多线程界面刷新
- C#值类型与引用类型间转换
- 编写高质量代码改善C#程序的157个建议——建议69:应使用finally避免资源泄漏
- c# WindowsService 的发布
- 编写高质量代码改善C#程序的157个建议——建议68:从System.Exception或其他常见的基本异常中派生异常
- C#中值类型和引用类型的区别深度分析
- C# Powerpoint 禁止或允许修改形状
- C#搜索文字在文件及文件夹中出现位置的方法
- 编写高质量代码改善C#程序的157个建议——建议67:慎用自定义异常
- 设计模式C#实现(二)——适配器模式
- C#多线程同步事件及等待句柄
- C#统计C、C++及C#程序代码行数的方法
- C# 未将对象引用设置到对象的实例
- C#中 哪些是值类型 哪些是引用类型
- c# 二元表达式
- 大白话系列之C#委托与事件讲解大结局
- C#使用RSA证书文件加密和解密示例(任意长度的内容)
- 大白话系列之C#委托与事件讲解(三)
- 大白话系列之C#委托与事件讲解(二)