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. A JavaFX based Game Authoring System

    http://www.mirkosertic.de/doku.php/javastuff/javafxgameauthoring ——————————————————————————————————— ...

  2. C# 多线程参数的使用

    一个参数: Thread.Start方法可以带一个参数: public static void Main() { Thread t = new Thread(new ParameterizedThre ...

  3. mysql kill操作

    KILL语法 KILL [CONNECTION | QUERY] thread_id 每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运 ...

  4. 一键安装GitLab7

    1. Install and configure the necessary dependencies If you install Postfix to send email please sele ...

  5. 在ASP.NET MVC中验证checkbox 必须选中 (Validation of required checkbox in Asp.Net MVC)

    转载自 http://blog.degree.no/2012/03/validation-of-required-checkbox-in-asp-net-mvc/ Why would you want ...

  6. redis安装配置与测试

    phpredis: https://github.com/nicolasff/phpredis http://www.cnblogs.com/ikodota/archive/2012/03/05/ph ...

  7. mysql无法启动 mysqld process already exists

    1.提示:A mysqld process already exists ps 命令用于查看当前正在运行的进程. grep 是搜索 例如: ps -ef | grep mysql 表示查看所有进程里 ...

  8. Oracle 分区字段数据更新

    分区字段是不允许进行update操作的,如果有对分区字段行进update,就会报错——ORA-14402:更新分区关键字列将导致分区的更改.  可以通过打开表的row movement属性来允许对分区 ...

  9. Instant Buy Android API Tutorial

    转自:https://developers.google.com/wallet/instant-buy/android/tutorial This tutorial guides you throug ...

  10. 【转】Google推荐的命名规则——Android图片资源

    http://blog.csdn.net/yy1300326388/article/details/45443477 1.译 资产类型 前缀 例子 图标 ic_ ic_star.png 启动图标 ic ...