使用C#实现读取/写入Excel表
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表的更多相关文章
- 用C#写入Excel表并保存
想用C#操作Excel表,首先要做一些准备工作. 如果要操作 microsoft office Excel 2003表,就需要引入Microsoft office 11.0 object librar ...
- 28、python3.7(windows)将ORACLE11gR2中的数据取出写入excel表
28.1.下载python的离线扩展模块: 1.windows下python的离线扩展模块下载地址为: https://www.lfd.uci.edu/~gohlke/pythonlibs/ 提示: ...
- JXL读取写入excel表格数据
问题描述: 使用java的jxl包创建.写入excel表格数据 问题解决: (1)说明 (2)写入execel数据 注: 以上是写入数据需要调用的函数接口 注: 具体接口调用过程,如上所示 (3)读取 ...
- python统计目录和目录下的文件,并写入excel表
运营那边提出需求,有些媒体文件需要统计下 目录结构大概是这样的 每个目录下面都有很多文件,目录下面没子目录 我这里是模拟下创建的目录和文件,和运营那边说的目录结构都是一致的 想最终统计结果如下格式 我 ...
- JXL读取,写入Excel
JXL读取,写入Excel2003 相关阅读:poi 读写excel2003:http://www.cnblogs.com/gavinYang/p/3576739.htmlpoi 读写excel200 ...
- Python+Selenium进行UI自动化测试项目中,常用的小技巧3:写入excel表(python,xlsxwriter)
我们在项目中可能用到excel表生成,下面的代码就是对excel表的操作: import xlsxwriter import datetime class write_excel(): def __i ...
- POI读取/写入Excel文件
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- sql sever读取写入Excel总结
主要用到openrowset,opendatasource系统函数,这两个函数任意一个都能完成任务 用这种方法可以实现Excel和sqlserver表之间的相互导入导出. 如果使用openrowset ...
- python3 读取写入excel操作-win32com
前面有写一篇是用xlrd操作excel的,这一篇是使用win32com来进行操作excel,个人推荐使用win32com. 要使用win32com组件,也需要先导入win32com包. # -*- c ...
随机推荐
- http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-open-and-save-it-using-dsoframer.aspx
http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-op ...
- Dependency Injection学习笔记
component把需要依赖者(CoffeeMaker)和供应提供者(Heater, Pump)联系起来 使用 区别:上的的依赖是内部创建的,下面的依赖是外面传进来的 注入方式
- 删除Ngnix 日志
删除Ngnix日志的脚本 #!/bin/bash #初始化 LOGS_PATH=$(pwd)/logs YESTERDAY=$(date -d "yesterday" +%Y-%m ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 3254 Corn Fields (状压dp)
题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...
- Objective-C 学习记录4
字符串的一些方法使用: 1.创建字典的NSString可变字符串,和NSMutableString不可变字符串.都是objective的对象. char *str是字母数组. 2.字符串格式化:str ...
- EF小节
EF学习笔记——生成自定义实体类 http://blog.csdn.net/leftfist/article/details/24889819 --工具: 1.entity developer 2.D ...
- ENTBOOST 2014.180L 发布,开源企业IM免费企业即时通讯
ENTBOOST,VERSION 2014.180 Linux版本发布,主要增加企业IM应用集成功能,完善安卓SDK功能及部分BUG修正: 7/1(明天)发布Windows版本,敬请关注! ENTBO ...
- USB Device Finder
http://www.velleman.eu/images/tmp/usbfind.c #ifdef __cplusplus extern "C" { #endif #includ ...
- js页面文字选中后分享到新浪微博实现
demo您可以狠狠地点击这里:js文字选中分享到新浪微博demo 方法与代码 选中即分享的功能看上去比较高级,其实实现是相当简单的.其中的会让人头大,一般人也不感兴趣的原理这里就直接跳过.这个js文字 ...