代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
c# Excel的基本操作
作者:
Dezai.CN
/ 发布于
2011/8/3
/
865
c#实现对Excel的Open、Save、合并单元格、单元格赋值的操作
<div> ExcelHelper代码: <div class="cnblogs_code"> <pre><span style="color: rgb(0,128,128)"> 1</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> ExcelHelper</span><span style="color: rgb(0,128,128)"> 2</span> <span style="color: rgb(0,0,0)"> {</span><span style="color: rgb(0,128,128)"> 3</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> SaveData()</span><span style="color: rgb(0,128,128)"> 4</span> <span style="color: rgb(0,0,0)"> {</span><span style="color: rgb(0,128,128)"> 5</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filePath </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Environment.CurrentDirectory </span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">\\Data</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)"> 6</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">Directory.Exists(filePath))</span><span style="color: rgb(0,128,128)"> 7</span> <span style="color: rgb(0,0,0)"> {</span><span style="color: rgb(0,128,128)"> 8</span> <span style="color: rgb(0,0,0)"> Directory.CreateDirectory(filePath);</span><span style="color: rgb(0,128,128)"> 9</span> <span style="color: rgb(0,0,0)"> }</span><span style="color: rgb(0,128,128)">10</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> fileName </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.Format(</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">{0}\\{1}.xls</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">, filePath, DateTime.Now.ToShortDateString());</span><span style="color: rgb(0,128,128)">11</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,128)">12</span> <span style="color: rgb(0,0,0)"> Excel.Application excelApp </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Excel.ApplicationClass();</span><span style="color: rgb(0,128,128)">13</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">新建工作簿</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">14</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)">(</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">File.Exists(fileName))</span><span style="color: rgb(0,128,128)">15</span> <span style="color: rgb(0,0,0)"> {</span><span style="color: rgb(0,128,128)">16</span> <span style="color: rgb(0,0,0)"> Excel.Workbook workBookNew </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> excelApp.Workbooks.Add(</span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">);</span><span style="color: rgb(0,128,128)">17</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">新建工作表</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">18</span> <span style="color: rgb(0,0,0)"> Excel.Worksheet worksheetNew </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> workBookNew.ActiveSheet </span><span style="color: rgb(0,0,255)">as</span><span style="color: rgb(0,0,0)"> Excel.Worksheet;</span><span style="color: rgb(0,128,128)">19</span> <span style="color: rgb(0,0,0)"> workBookNew.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">, Missing.Value);</span><span style="color: rgb(0,128,128)">20</span> <span style="color: rgb(0,0,0)"> }</span><span style="color: rgb(0,128,128)">21</span> <span style="color: rgb(0,0,0)"> Excel.Workbook workBook </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> excelApp.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">, Missing.Value, Missing.Value, </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">, Missing.Value, </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">);</span><span style="color: rgb(0,128,128)">22</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,128)">23</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">新建工作表</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">24</span> <span style="color: rgb(0,0,0)"> Excel.Worksheet worksheet </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> workBook.ActiveSheet </span><span style="color: rgb(0,0,255)">as</span><span style="color: rgb(0,0,0)"> Excel.Worksheet;</span><span style="color: rgb(0,128,128)">25</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,128)">26</span> <span style="color: rgb(0,0,0)"> Excel.Range r </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> worksheet.get_Range(</span><span style="color: rgb(0,128,128)">27</span> <span style="color: rgb(0,0,0)"> worksheet.Cells[</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">], worksheet.Cells[</span><span style="color: rgb(128,0,128)">3</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">3</span><span style="color: rgb(0,0,0)">]); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">选取单元格</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">28</span> <span style="color: rgb(0,0,0)"> r.MergeCells </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)">29</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,128)">30</span> <span style="color: rgb(0,0,0)"> r.Value2 </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">我爱中华</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">设置单元格内文本</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">31</span> <span style="color: rgb(0,0,0)"> r.Font.Name </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">黑体</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">设置字体</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">32</span> <span style="color: rgb(0,0,0)"> r.Font.Size </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">20</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">字体大小</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">33</span> <span style="color: rgb(0,0,0)"> r.Font.Bold </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加粗显示</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">34</span> <span style="color: rgb(0,0,0)"> r.HorizontalAlignment </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Excel.XlHAlign.xlHAlignCenter; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">水平居中</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">35</span> <span style="color: rgb(0,0,0)"> r.VerticalAlignment </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Excel.XlVAlign.xlVAlignCenter; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">垂直居中</span><span style="color: rgb(0,128,0)"></span><span style="color: rgb(0,128,128)">36</span> <span style="color: rgb(0,0,0)"></span><span style="color: rgb(0,128,128)">37</span> <span style="color: rgb(0,0,0)"> worksheet.Cells[</span><span style="color: rgb(128,0,128)">4</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">] </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">《歌唱祖国》</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)">38</span> <span style="color: rgb(0,0,0)"> worksheet.Cells[</span><span style="color: rgb(128,0,128)">5</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">] </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">《为中华崛起而读书》</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)">39</span> <span style="color: rgb(0,0,0)"> excelApp.DisplayAlerts </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)">40</span> <span style="color: rgb(0,0,0)"> excelApp.AlertBeforeOverwriting </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)">41</span> <span style="color: rgb(0,0,0)"> workBook.Save();</span><span style="color: rgb(0,128,128)">42</span> <span style="color: rgb(0,0,0)"> excelApp.Quit();</span><span style="color: rgb(0,128,128)">43</span> <span style="color: rgb(0,0,0)"> System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);</span><span style="color: rgb(0,128,128)">44</span> <span style="color: rgb(0,0,0)"> excelApp </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)">45</span> <span style="color: rgb(0,0,0)"> System.GC.Collect();</span><span style="color: rgb(0,128,128)">46</span> <span style="color: rgb(0,0,0)"> }</span><span style="color: rgb(0,128,128)">47</span> <span style="color: rgb(0,0,0)"> }</span><span style="color: rgb(0,128,128)">48</span> <span style="color: rgb(0,0,0)"> </span></pre> </div> Program 代码: <div class="cnblogs_code"> <pre><span style="color: rgb(0,128,128)">1</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> Program</span><span style="color: rgb(0,128,128)">2</span> <span style="color: rgb(0,0,0)"> {</span><span style="color: rgb(0,128,128)">3</span> <span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> Main(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">[] args)</span><span style="color: rgb(0,128,128)">4</span> <span style="color: rgb(0,0,0)"> {</span><span style="color: rgb(0,128,128)">5</span> <span style="color: rgb(0,0,0)"> Console.Title </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Excel Operation Example</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,128)">6</span> <span style="color: rgb(0,0,0)"> ExcelHelper.SaveData();</span><span style="color: rgb(0,128,128)">7</span> <span style="color: rgb(0,0,0)"> Environment.Exit(</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">);</span><span style="color: rgb(0,128,128)">8</span> <span style="color: rgb(0,0,0)"> }</span><span style="color: rgb(0,128,128)">9</span> <span style="color: rgb(0,0,0)"> }</span></pre> </div> 结果: <table style="width: 288px; border-collapse: collapse" border="0" cellspacing="0" cellpadding="0" x:str=""> <colgroup><col style="width: 54pt" span="4" width="72" /></colgroup> <tbody> <tr style="height: 14.25pt" height="19"> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); width: 162pt; height: 42.75pt; border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216)" class="xl22" height="57" rowspan="3" width="216" colspan="3"><strong><span style="font-family: 黑体; font-size: x-large" face="黑体" size="6"><font size="5">我爱中华</font></span></strong></td> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); width: 54pt; border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216)" width="72"></td> </tr> <tr style="height: 14.25pt" height="19"> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); height: 14.25pt; border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216)" height="19"></td> </tr> <tr style="height: 14.25pt" height="19"> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); height: 14.25pt; border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216)" height="19"></td> </tr> <tr style="height: 14.25pt" height="19"> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); height: 14.25pt; border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216); mso-ignore: colspan" height="19" colspan="2"><span style="font-family: 宋体; font-size: small" face="宋体" size="3"><font size="2">《歌唱祖国》</font></span></td> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216)"></td> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216)"></td> </tr> <tr style="height: 14.25pt" height="19"> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); height: 14.25pt; border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216)" height="19"></td> <td style="border-bottom-color: rgb(236,233,216); background-color: transparent; border-top-color: rgb(236,233,216); border-right-color: rgb(236,233,216); border-left-color: rgb(236,233,216); mso-ignore: colspan" colspan="3"><span style="font-family: 宋体; font-size: small" face="宋体" size="3"><font size="2">《为中华崛起而读书》</font></span></td> </tr> </tbody> </table> </div>
试试其它关键字
Excel的基本操作
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3