C#实现写入Excel表

using System;
using System.Reflection;
using System.IO;
using Microsoft.Office.Interop.Excel; namespace Test
{
class Program
{
public static bool WriteXls(string filename)
{
//启动Excel应用程序
Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
_Workbook book = xls.Workbooks.Add(Missing.Value); //创建一张表,一张表可以包含多个sheet //如果表已经存在,可以用下面的命令打开
//_Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); _Worksheet sheet;//定义sheet变量
xls.Visible = false;//设置Excel后台运行
xls.DisplayAlerts = false;//设置不显示确认修改提示 for (int i = ; i < ; i++)//循环创建并写入数据到sheet
{
try
{
sheet = (_Worksheet)book.Worksheets.get_Item(i);//获得第i个sheet,准备写入
}
catch (Exception ex)//不存在就增加一个sheet
{
sheet = (_Worksheet)book.Worksheets.Add(Missing.Value, book.Worksheets[book.Sheets.Count], , Missing.Value);
}
sheet.Name = "第" + i.ToString() + "页";//设置当前sheet的Name
for (int row = ; row < ; row++)//循环设置每个单元格的值
{
for (int offset = ; offset < ; offset++)
sheet.Cells[row, offset] = "( " + row.ToString() + "," + offset.ToString() + " )";
}
}
//将表另存为
book.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //如果表已经存在,直接用下面的命令保存即可
//book.Save(); book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
xls.Quit();//Excel程序退出
//sheet,book,xls设置为null,防止内存泄露
sheet = null;
book = null;
xls = null;
GC.Collect();//系统回收资源
return true;
}
static void Main(string[] args)
{
string Current;
Current = Directory.GetCurrentDirectory();//获取当前根目录
WriteXls(Current + "\\test.xls");
}
}
} C#实现读取Excel表 using System;
using System.Reflection;
using System.IO;
using Microsoft.Office.Interop.Excel; namespace Test
{
class Program
{
public static Array ReadXls(string filename,int index)//读取第index个sheet的数据
{
//启动Excel应用程序
Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
//打开filename表
_Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); _Worksheet sheet;//定义sheet变量
xls.Visible = false;//设置Excel后台运行
xls.DisplayAlerts = false;//设置不显示确认修改提示 try
{
sheet = (_Worksheet)book.Worksheets.get_Item(index);//获得第index个sheet,准备读取
}
catch (Exception ex)//不存在就退出
{
Console.WriteLine(ex.Message);
return null;
}
Console.WriteLine(sheet.Name);
int row = sheet.UsedRange.Rows.Count;//获取不为空的行数
int col = sheet.UsedRange.Columns.Count;//获取不为空的列数
Array value = (Array)sheet.get_Range(sheet.Cells[, ], sheet.Cells[row, col]).Cells.Value2;//获得区域数据赋值给Array数组,方便读取 book.Save();//保存
book.Close(false, Missing.Value, Missing.Value);//关闭打开的表
xls.Quit();//Excel程序退出
//sheet,book,xls设置为null,防止内存泄露
sheet = null;
book = null;
xls = null;
GC.Collect();//系统回收资源
return value;
} static void Main(string[] args)
{
string Current;
Current = Directory.GetCurrentDirectory();//获取当前根目录
Array Data = ReadXls(Current + "\\test.xls", );//读取test.xlsx的第一个sheet表
foreach (string temp in Data)
Console.WriteLine(temp);
Console.ReadKey();
}
}
} Excel表的一些特性操作 range.NumberFormatLocal = "@"; //设置单元格格式为文本
range = (Range)worksheet.get_Range("A1", "E1"); //获取Excel多个单元格区域
range.Merge(Type.Missing); //单元格合并动作
worksheet.Cells[, ] = "Excel单元格赋值"; //Excel单元格赋值
range.Font.Size = ; //设置字体大小
range.Font.Underline=true; //设置字体是否有下划线
range.Font.Name="黑体"; 设置字体的种类
range.HorizontalAlignment=XlHAlign.xlHAlignCenter; //设置字体在单元格内的对其方式
range.ColumnWidth=; //设置单元格的宽度
range.Cells.Interior.Color=System.Drawing.Color.FromArgb(,,).ToArgb(); //设置单元格的背景色
range.Borders.LineStyle=; //设置单元格边框的粗细
range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb()); //给单元格加边框
range.EntireColumn.AutoFit(); //自动调整列宽
range.HorizontalAlignment= xlCenter; // 文本水平居中方式
range.VerticalAlignment= xlCenter //文本垂直居中方式
range.WrapText=true; //文本自动换行
range.Interior.ColorIndex=; //填充颜色为淡紫色
range.Font.Color=clBlue; //字体颜色
xlsApp.DisplayAlerts=false; //保存Excel的时候,不弹出是否保存的窗口直接进行保存
workbook.SaveCopyAs(temp); //填入完信息之后另存到路径及文件名字

使用C#实现读取/写入Excel表的更多相关文章

  1. 用C#写入Excel表并保存

    想用C#操作Excel表,首先要做一些准备工作. 如果要操作 microsoft office Excel 2003表,就需要引入Microsoft office 11.0 object librar ...

  2. 28、python3.7(windows)将ORACLE11gR2中的数据取出写入excel表

    28.1.下载python的离线扩展模块: 1.windows下python的离线扩展模块下载地址为: https://www.lfd.uci.edu/~gohlke/pythonlibs/ 提示: ...

  3. JXL读取写入excel表格数据

    问题描述: 使用java的jxl包创建.写入excel表格数据 问题解决: (1)说明 (2)写入execel数据 注: 以上是写入数据需要调用的函数接口 注: 具体接口调用过程,如上所示 (3)读取 ...

  4. python统计目录和目录下的文件,并写入excel表

    运营那边提出需求,有些媒体文件需要统计下 目录结构大概是这样的 每个目录下面都有很多文件,目录下面没子目录 我这里是模拟下创建的目录和文件,和运营那边说的目录结构都是一致的 想最终统计结果如下格式 我 ...

  5. JXL读取,写入Excel

    JXL读取,写入Excel2003 相关阅读:poi 读写excel2003:http://www.cnblogs.com/gavinYang/p/3576739.htmlpoi 读写excel200 ...

  6. Python+Selenium进行UI自动化测试项目中,常用的小技巧3:写入excel表(python,xlsxwriter)

    我们在项目中可能用到excel表生成,下面的代码就是对excel表的操作: import xlsxwriter import datetime class write_excel(): def __i ...

  7. POI读取/写入Excel文件

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  8. sql sever读取写入Excel总结

    主要用到openrowset,opendatasource系统函数,这两个函数任意一个都能完成任务 用这种方法可以实现Excel和sqlserver表之间的相互导入导出. 如果使用openrowset ...

  9. python3 读取写入excel操作-win32com

    前面有写一篇是用xlrd操作excel的,这一篇是使用win32com来进行操作excel,个人推荐使用win32com. 要使用win32com组件,也需要先导入win32com包. # -*- c ...

随机推荐

  1. 了解shell

    1. shell 脚本文件第一行:    #!/bin/sh 或 #!/bin/bash "#!"  又称为纪数,在执行bash脚本的时候,内核会根据它来确定该用哪个程序来解释脚本 ...

  2. Connecting Physics Bodies

    [Connecting Physics Bodies] The kinds of joints you can create in Sprite Kit. You add or remove join ...

  3. 左手坐标系&右手坐标系

    [左手坐标系&右手坐标系] 左手坐标系的正方向.从原点看到某轴正向时,逆时针即为正方向.相反地,从某轴正方向看看原点时,为顺时针即为正方向. 如果判断左手坐标系下叉积的方向.如果A.B向量首尾 ...

  4. 一种将Region转为Polyline的方法

    在AutoCAD.NET二次开发中,如果要将面域转为Polyline主要有以下几种方式: 1.使用Explode将面域炸成Line和Arc,然后再串起来,此方法可用于AutoCAD2007开始的所有版 ...

  5. 找回使用过的QQ头像

    多么渴望那双眼睛能在万紫千红中发现自己:然而眼睛从来就不曾对自己留意:于是换种落寞再次接受垂直打击:然后然后的然后尼玛再换个逗比的头像证明老资无所谓老资无所谓嘿嘿: 我们换头像的时候,发现之前的一个最 ...

  6. POJ 3169 Layout (差分约束)

    题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...

  7. C++开发必看 四种强制类型转换的总结 [转]

    一.C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:     TYPE b = (TYPE)a 二.C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. co ...

  8. winrar激活

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-02-11) 新建记事本文件(txt文件),然后将文件另存为以 rarreg.key 为文件名的文件(当然由于设置的不同,可能 ...

  9. android如何实现开机自动启动Service或app

    第一步:首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动的Service或app. import and ...

  10. CORTEX -M3 : Registers in depth

    http://www.zembedded.com/cortex-m3-registers-in-depth/ Thanks for the overwhelm response you show in ...