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();
}

C# 创建Excel并写入内容的更多相关文章

  1. 【HDFS API编程】查看HDFS文件内容、创建文件并写入内容、更改文件名

    首先,重点重复重复再重复: /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 ...

  2. HDFS上创建文件、写入内容

    1.创建文件 hdfs dfs -touchz /aaa/aa.txt 2.写入内容 echo "<Text to append>" | hdfs dfs -appen ...

  3. c# 创建excel表头及内容

    主要通过ajax调用函数Getinfo 1.定义表dh DataTable dh = new DataTable(); 2.创建表头 public void CreateCol(string Colu ...

  4. NPOI 创建Excel,数据读取与写入

    <1> using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  5. 使用Java创建Excel,并添加内容

    使用Java创建Excel,并添加内容 一.依赖的Jar包 jxl.jar,使用jxl操作Excel Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Ex ...

  6. (最全最灵活地)利用Jxl工具包实现Excel表的内容读取 、写入(可向已有表中追加数据)

    1.引子 (1)读取 Jxl工具比较强大,可以方便地实现Excel表的读取和写入.另一款工具Poi也具有相似的功能,并且功能更多,运用也相对复杂.Poi读取Excel表内容时,需要先判断其内容格式,如 ...

  7. 关于java中创建文件,并且写入内容

    以下内容完全为本人原创,如若转载,请注明出自:http://www.cnblogs.com/XiOrang/ 前两天在项目中因为要通过http请求获取一个比较大的json数据(300KB左右)并且保存 ...

  8. Java 创建Excel并逐行写入数据

    package com.xxx.common.excel; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...

  9. 如何使用Java创建Excel(.xls 和 .xlsx)文件 并写入数据

    1,需要依赖的jar包, <!-- POI(operate excel) start --> <!-- the version of the following POI packag ...

随机推荐

  1. hyperv虚拟机网络速度慢问题的解决办法

    服务器安装了windows2012R2进行虚拟化,虚拟机也是用的是windows2012R2的操作系统,这样可以一次激活对应的虚拟机. 在使用虚拟机的过程中发现问题,虚拟机主机的网速正常,无论是ftp ...

  2. PHP学习之[第01讲]开启PHP学习之路,融入新互联网时代

    小编本身现在是手机应用客户端开发者,包括iOS和Android. 学习PHP有两个目的: 1.为移动端提供服务: 2.向后台项目的架构方面学习.

  3. c# 根据自定义Attribute排序

    add a class: public class ExportAttribute : Attribute {     public int FieldOrder { get; set; }      ...

  4. Android-Uiautomator:[5]停止monkey测试

    方法/步骤 1 其实停止很简单,无非就是结束掉monkey的进程即可 如何停止呢 2 ps命令  查找uiautomator的进程 打开cmd命令行窗口 输入: adb shell ps | grep ...

  5. [Angular 2] Managing State in RxJS with StartWith and Scan

    The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves ...

  6. [转] Python 模块学习:os模块

    一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.(一语中的) 二.常用方法 1.os.name 输出字符串指示正在使用的平台 ...

  7. 去除jquery.min.map 404错误信息

    调试中出现了 jquery.min.map 404 (Not Found) 的js错误信息: 那么jquery.min.map到底是个什么呢? JQuery 官方解释 从 jQuery 1.9.0 版 ...

  8. Android(java)学习笔记250:ContentProvider使用之获得系统联系人信息02(掌握)

    1.重要: 系统删除一个联系人,默认情况下并不是把这个联系人直接删除掉了,只是做了一个标记,标记为被删除. 2.前面一讲说过了如何获取系统联系人信息(通过ContentProvider),获取联系人信 ...

  9. yii [error] [exception.CHttpException.404] exception 'CHttpException' with message 'Unable to resolve the request "favicon.ico".'

    yii使用中,发现runtime文件夹下出现这个错误信息 解决办法:在生成的APP程序根目录下建.htaccess文件(前提是需要开启apache重写,具体如何开,查资料咯) 然后配置如下 <I ...

  10. spring06Aop

    1.实现前置增强 必须实现接口MethodBeforeAdvice接口 创建对应的文件 public interface Animal {//主业务接口 void eat(); //目标方法 void ...