ExcelHelper.docx

上传人:b****6 文档编号:8851995 上传时间:2023-02-02 格式:DOCX 页数:30 大小:32.70KB
下载 相关 举报
ExcelHelper.docx_第1页
第1页 / 共30页
ExcelHelper.docx_第2页
第2页 / 共30页
ExcelHelper.docx_第3页
第3页 / 共30页
ExcelHelper.docx_第4页
第4页 / 共30页
ExcelHelper.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

ExcelHelper.docx

《ExcelHelper.docx》由会员分享,可在线阅读,更多相关《ExcelHelper.docx(30页珍藏版)》请在冰豆网上搜索。

ExcelHelper.docx

ExcelHelper

C#操作Excel文件

网上很多读取excel文件的方法都是通过数据库连接把excel文件当做一个数据库执行读取,这样的方式效率较高。

但是无法对单个单元格进行读写操作。

因项目需要,要求能创建空白excel文件,指定sheet的名称,设置单元格的颜色等,整理了一个Excel操作类与大家共享。

注:

要求电脑上必须安装office2007及以上版本。

项目里添加对office的引用。

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Reflection;

usingMSExcel=Microsoft.Office.Interop.Excel;

usingSystem.Collections;

usingSystem.IO;

usingLaisonTech.CommonBLL;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Data.OleDb;

usingSystem.Diagnostics;

usingSystem.Runtime.InteropServices;

namespaceLaisonTech.DataAccessLayer

{

///

///日期:

2017-03-16

///作者:

zhengjf

///邮箱:

25035844@

///功能:

Excel文件操作

///用法:

///1.创建本类对象

///2.CreateNewExcel创建空白Excel文件;

///3.Open打开excel文件;

///GetRowCount获取指定表格的行数

///GetColumnCount获取指定表格的列数

///GetSheet把指定表格读取到datatable里

///GetSheets把所有表格读取到datatable列表里

///Read读取指定表格指定单元内容

///SetCellsBackColor设置单元格颜色

///SetRowBackColor设置某一行的内容

///Write写入内容到单元格/把1个datatable写入到单元格

///Close关闭已经打开的文件

///4.退出之前必须先调用Dispose

///注意事项:

各参数里的下标都是从0开始.函数内部会自动转换为Excel所需的1开始

///

publicclassExcelHelper

{

#region属性

///

///当前是否已经打开文件

///

publicBooleanOpened

{

get;

privateset;

}

///

///文件的sheet名称列表

///

publicListSheetsNameList

{

get

{

returnm_sheetnamelist;

}

privateset{}

}

///

///文件的所有sheet对象集合

///

publicMSExcel.SheetsSheets

{

get

{

returnm_sheets;

}

privateset{}

}

privateSystem.Globalization.CultureInfom_oldCI=System.Threading.Thread.CurrentThread.CurrentCulture;

#endregion

#region内部成员

privateStringm_FileName=String.Empty;

//整个Excel程序

privateMSExcel.ApplicationClassm_excelApp=null;

//一个文件

privateMSExcel.WorkbookClassm_file=null;

//工作表

privateMSExcel.Sheetsm_sheets=null;

///

///文件里的工作表名称列表

///

privateListm_sheetnamelist=null;

///

///设置sheet不能超过26列

///

publicconstInt32Const_Colcount=26;

#endregion

#region内部函数

///

///执行打开文件的操作,不加异常保护

///

///

///

privateBooleanExecuteOpen(Stringfilename)

{

m_excelApp=newMSExcel.ApplicationClass();

m_excelApp.Visible=false;

m_excelApp.UserControl=false;

Opened=true;

m_file=(MSExcel.WorkbookClass)m_excelApp.Workbooks.Open(filename,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing);

if(m_file==null)

{

Close();

returnfalse;

}

m_sheets=m_file.Worksheets;

if(m_sheets.Count<1)

{

Close();

returnfalse;

}

m_sheetnamelist=newList();

foreach(MSExcel.Worksheetsheetinm_sheets)//m_file.Sheets)

{

m_sheetnamelist.Add(sheet.Name);

}

returntrue;

}

#endregion

#region公开方法

publicExcelHelper()

{

}

///

///执行打开文件的操作,加异常保护

///打开失败则自动关闭

///

///

///

publicBooleanOpen(Stringfilename)

{

if(Opened)

{

if(m_FileName.Equals(filename))

{

returntrue;

}

Close();

}

Booleanbl=FileProcessor.FileExist(filename);

if(!

bl)

{

returnfalse;

}

FileProcessor.SetFileReadOnly(filename,false);

m_FileName=filename;

m_oldCI=System.Threading.Thread.CurrentThread.CurrentCulture;

System.Threading.Thread.CurrentThread.CurrentCulture=newSystem.Globalization.CultureInfo("en-US");

Stringmsg="";

try

{

bl=ExecuteOpen(filename);

}

catch(Exceptionex)

{

//出错,先关闭excel,防止驻留在线程里

Close();

msg=ex.Message;

}

//如果有异常,抛出异常,以便界面知道

if(msg.Length>0)

{

thrownewException(msg);

}

returnbl;

}

///

///关闭

///

///

///

publicBooleanClose()

{

//恢复原来的区域语言

System.Threading.Thread.CurrentThread.CurrentCulture=m_oldCI;

if(!

Opened)

{

returnfalse;

}

m_FileName=String.Empty;

Opened=false;

objectsaved=true;

try

{

if(m_file!

=null)

{

m_file.Close(saved);

}

}

catch(Exceptionex)

{

DebugHelper.Print(ex.Message);

}

try

{

if(m_excelApp!

=null)

{

m_excelApp.Workbooks.Close();

m_excelApp.Quit();

}

}

catch(Exceptionex)

{

DebugHelper.Print(ex.Message);

}

m_file=null;

m_excelApp=null;

//操作过Excel对象后,必须强制垃圾回收.否则无法退出Exccel进程

CommonCompute.GCCollect();

returntrue;

}

publicvoidDispose()

{

if(m_excelApp==null)

return;

m_excelApp.Quit();

m_excelApp=null;

}

///

///保存文件

///

///

publicBooleanSave()

{

if(!

Opened)

{

returnfalse;

}

try

{

m_file.Save();

}

catch(System.Exceptionex)

{

DebugHelper.Print("Saveerror:

"+ex.Message);

returnfalse;

}

returntrue;

}

///

///获取指定序号的表格里,指定行列的信息

///

///表格,从0开始

///行,从0开始

///列,从0开始

///

publicBooleanRead(intsheetidx,introwidx,intcolidx,outStringvalue)

{

value=String.Empty;

if(!

Opened)

{

returnfalse;

}

if(sheetidx<0||sheetidx>=this.m_sheetnamelist.Count)

{

returnfalse;

}

MSExcel.Worksheetsheet=(MSExcel.Worksheet)m_sheets[sheetidx+1];

value=((MSExcel.Range)sheet.Cells[rowidx+1,colidx+1]).Text.ToString();

//MSExcel.Rangerange=(MSExcel.Range)sheet.Cells[rowidx+1,colidx+1];

//value=range.Cells[1,1].ToString();

returntrue;

}

///

///获取指定名称的表格里,指定行列的信息

///

///

///从0开始

///从0开始

///

publicBooleanRead(Stringsheetname,introwidx,intcolidx,outStringvalue)

{

value=String.Empty;

Int32sheetidx=m_sheetnamelist.IndexOf(sheetname);

if(sheetidx<0)

{

returnfalse;

}

Booleanbl=Read(sheetidx,rowidx+1,colidx+1,outvalue);

returnbl;

}

///

///修改某个单元格的颜色

///

///

///

///

///

///

privateBooleanSetCellsBackColor(MSExcel.Worksheetsheet,Int32rowidx,

Int32colidx,Colorcolor)

{

if(colidx>26)

{

returnfalse;

}

Int32rownum=rowidx+1;

charcolchar=(char)('A'+colidx);

Stringcell=colchar+rownum.ToString();

MSExcel.Ranger=sheet.get_Range(cell,cell);

r.Interior.Color=System.Drawing.ColorTranslator.ToOle(color);

returntrue;

}

///

///设置行的颜色

///

///

///

///

///

///

///

///

publicBooleanSetRowBackColor(Int32sheetidx,introwidx,Colorcolor)

{

if(sheetidx<0||sheetidx>=m_sheets.Count)

{

returnfalse;

}

MSExcel.Worksheetsheet=(MSExcel.Worksheet)m_sheets[sheetidx+1];

if(rowidx<0||rowidx>=sheet.Rows.Count)

{

returnfalse;

}

rowidx++;

Int32colcnt=GetColumnCount(sheetidx);

charstartcolchar='A';

charendcolchar=(char)('A'+colcnt);//最后一列

Stringstartcell=startcolchar+rowidx.ToString();//AN,N为列序号.比如A3

Stringendcell=endcolchar+rowidx.ToString();//MN,M为末尾列标题.比如G3

MSExcel.Ranger=sheet.get_Range(startcell,endcell);

r.Interior.Color=System.Drawing.ColorTranslator.ToOle(color);

returntrue;

}

///

///设置单元格的颜色

///

///

///

///

///

///

///

///

publicBooleanSetCellsBackColor(Int32sheetidx,intstartrowidx,intstartcolidx,intendrowidx,

intendcolidx,Colorcolor)

{

if(startcolidx>=26||endcolidx>=26)

{

returnfalse;

}

if(startrowidx>endrowidx)

{

returnfalse;

}

if(m_sheetnamelist==null||m_sheetnamelist.Count<1||

sheetidx<0||sheetidx>=m_sheetnamelist.Count)

{

returnfalse;

}

MSExcel.Worksheetsheet=(MSExcel.Worksheet)m_sheets[sheetidx+1];

Int32strartrownum=startrowidx+1;

Int32endrownum=endrowidx+1;

charstartcolchar=(char)('A'+startcolidx);

charendcolchar=(char)('A'+endcolidx);

Stringstartcell=startcolchar+strartrownum.ToString();

Stringendcell=endcolchar+endrownum.ToString();

MSExcel.Ranger=sheet.get_Range(startcell,endcell);

r.Interior.Color=System.Drawing.ColorTranslator.ToOle(color);

returntrue;

}

///

///设置指定表格内容

///

///从0开始

///从0开始

///从0开始

///

///

publicBooleanWrite(Int32sheetidx,introwidx,intcolidx,Stringvalue)

{

//value=String.Empty;

if(m_sheetnamelist==null||m_sheetnamelist.Count<1||

sheetidx<0||sheetidx>=m_sheetnamelist.Count)

{

returnfalse;

}

MSExcel.Worksheetsheet=(MSExcel.Worksheet)m_sheets[sheetidx+1];

if(rowidx<0||rowidx>=sheet.Cells.Rows.Count)

{

returnfalse;

}

if(colidx<0||colidx>=sheet.Cells.Columns.Count)

{

returnfalse;

}

sheet.Cells[rowidx+1,colidx+1]=value;

returntrue;

}

///

///设置指定表格内

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1