- 在项目菜单上,单击添加引用。
- 在 COM 选项卡上,找到 Microsoft Excel 对象库,然后单击选择。
添加命名空间 using Excel = Microsoft.Office.Interop.Excel;
关于C#对Excel的操作,我已写好类库,可去https://download.csdn.net/download/weixin_42314225/10726578 下载
/*****************************************************************************
File name: ExcelHelper.cs
Description: 该模块作为C#操作Excel的基类使用
Function List:
ExcelHelper(string template, string path, bool Visable) 构造函数,调用CreatExcel
void CreatExcel(string template,string path,bool Visable) 创建Excel表格
void SelectSheet(int index) 选择sheet工作表
void CellSet(int row,int col,string value) 向坐标为(row,col)的单元格写入字符串“value”
string CellGet(int row, int col) 读出坐标为(row,col)的单元格中的内容
void SaveExcel() 保存Excel文件
void CloseExcel() 关闭Excel文件
void SetCellColor(string begin,string end,int red,int green,int blue) 设置单元格颜色
void CopyContent(int from_row, int from_column, int end_row, int end_column, int goal_row,int goal_column) 拷贝单元格内容至新的单元格
void SetColumnRange(int start,int end,int column,int[] array) 单元格列赋值
Author: Hanc<hanchao@xinfoo-ic.com>
Version: V1.1
Date: 2018.10.17
History:
*****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Drawing;
using X1CPMW;
namespace HCLib.Office
{
public class ExcelHelper
{
private Excel.Application oXL;
private Excel._Workbook oWB;
private Excel.Sheets sheets;
private Excel._Worksheet workSheet;
private Excel.Range oRg;
string sPath = "";
public ExcelHelper(string template, string path, bool Visable)
{
CreatExcel(template, path, Visable);
}
/*****************************************************************
*Func: CreatExcel
*Description: 创建Excel表格
*Input:
* template :Excel报表模板所在路径,若输入值为“null”,则不使用
* 模板
* path: 文件保存路径
* Visable : 是否显示Excel报表
*Output:
* void
*Others:
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.12
*****************************************************************/
public void CreatExcel(string template,string path,bool Visable)
{
oXL = new Excel.Application();
oXL.Visible = Visable;
sPath = path;
try
{
//优先打开path路径指定的文件
if (File.Exists(sPath))
{
oWB = (Excel._Workbook)(oXL.Workbooks.Open(sPath));
}
//若文件不存在,则根据所选定的Excel模板,在内存中创建一个该模板的Excel文件
else if (File.Exists(template))
{
oWB = (Excel._Workbook)(oXL.Workbooks.Add(template));
}
else
{
throw new Exception("模板和文件路径不存在");
}
sheets = oWB.Sheets;
}
catch(Exception ex)
{
throw new Exception("ExcelHelper: "+ex.Message);
}
}
/*****************************************************************
*Func: SelectSheet
*Description: 选择sheet工作表
*Input:
* index :用来选择工作表的索引值
*Output:
* void
*Others:
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.12
*****************************************************************/
public void SelectSheet(int index)
{
try
{
workSheet = (Excel._Worksheet)sheets[index];
}
catch(Exception ex)
{
throw new Exception("Excel [sheet 索引越界]," + ex.Message);
}
}
/*****************************************************************
*Func: CellSet
*Description: 向坐标为(row,col)的单元格写入字符串“value”
*Input:
* row :行坐标
* col :行坐标
* value :写入值
*Output:
* void
*Others:
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.12
*****************************************************************/
public void CellSet(int row,int col,string value)
{
workSheet.Cells[row, col] = value;
}
/*****************************************************************
*Func: CellGet
*Description: 读出坐标为(row,col)的单元格中的内容
*Input:
* row :行坐标
* col :行坐标
*Output:
* void
*return:
* 返回坐标Cell中的内容
*Others:
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.16
*****************************************************************/
public string CellGet(int row, int col)
{
return workSheet.Cells[row, col].Value;
}
/*****************************************************************
*Func: SaveExcel
*Description: 保存Excel文件
*Input:
* void
*Output:
* void
*Others:
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.15
*****************************************************************/
public void SaveExcel()
{
if (File.Exists(sPath))
{
oWB.Save();
}
else
{
oWB.SaveAs(sPath);
}
}
/*****************************************************************
*Func: CloseExcel
*Description: 关闭Excel文件
*Input:
* void
*Output:
* void
*Others:
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.15
*****************************************************************/
public void CloseExcel()
{
oWB.Close();
oXL.Quit();
}
/*****************************************************************
*Func: SetCellColor
*Description: 设置单元格颜色
*Input:
* begin:单元格范围起始位置
* end:单元格范围结束位置
* red、green、blue:
* Creates a Color structure from the specified 8-bit color values (red, green, and blue).
* The alpha value is implicitly 255 (fully opaque). Although this method allows a 32-bit
* value to be passed for each color component, the value of each component is limited to
* 8 bits.
**Output:
* void
*Others:
* 如将单元格"A3"设置为黄色,则传参SetCellColor("A3","A3",255,255,0)
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.15
*****************************************************************/
void SetCellColor(string begin,string end,int red,int green,int blue)
{
workSheet.get_Range(begin, end).Cells.Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.FromArgb(red,green,blue));
}
/*****************************************************************
*Func: CopyContent
*Description: 拷贝单元格内容至新的单元格
*Input:
* from_row:单元格范围起始行坐标
* from_column:单元格范围起始列坐标
* end_row:单元格范围结束行坐标
* end_column:单元格范围结束列坐标
* goal_row:目标位置行坐标
* goal_column:目标位置列坐标
**Output:
* void
*Others:
* 如将单元格第1行、第2列至第18行、第2列之间的内容拷贝至以第1行、
* 第3列为起始的单元格处,则传参CopyContent(1,2,18,2,1,3)
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.15
*****************************************************************/
public void CopyContent(int from_row, int from_column, int end_row, int end_column, int goal_row,int goal_column)
{
workSheet.Range[workSheet.Cells[from_row, from_column], workSheet.Cells[end_row, end_column]].
Copy(workSheet.Cells[goal_row, goal_column]);
}
/*****************************************************************
*Func: SetColumnRange
*Description: 单元格列赋值
*Input:
* start:起始行坐标
* end:结束行坐标
*Output:
* void
*Others:
*Author: Hanc<hanchao@xinfoo-ic.com>
*Date: 2018.10.16
*****************************************************************/
public void SetColumnRange(int start,int end,int column,int[] array)
{
try
{
if (end - start < 0)
throw new Exception("结束行坐标小于起始行坐标");
int[,] temp = new int[end - start + 1, 1];
for (int i = 0; i <= end - start; i++)
{
temp[i, 0] = array[i];
}
oRg = workSheet.Range[workSheet.Cells[start, column], workSheet.Cells[end, column]];
oRg.Value = temp;
}
catch(Exception ex)
{
throw new Exception("SetColumnRange" + ex.Message);
}
}
}
}