using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.Xml.Linq;
namespace Bowin.Common.Utility
{
public enum ExcelFormat
{
Excel95 = Excel.XlFileFormat.xlExcel7,
Excel95_97 = Excel.XlFileFormat.xlExcel9795,
Excel2000_03 = Excel.XlFileFormat.xlExcel8,
Excel2007 = Excel.XlFileFormat.xlExcel12,
Excel2010 = Excel.XlFileFormat.xlExcel4
}
///
/// 使用Excel2007 com封装
///
public class ExcelHelper
{
///
/// excel路径
///
public string FilePath { get; set; }
private Excel.Application oXL;
private Excel.Workbook oWB;
private bool isNew = false;
private bool isOpened = false;
public ExcelHelper(string a_filepath = "")
{
FilePath = a_filepath;
}
///
/// 打开目标excel
///
public void Open()
{
if (oXL != null)
{
throw new Exception("已打开一个Excel文件,请先关闭原文件后再打开新文件");
}
if (string.IsNullOrEmpty(FilePath))
{
throw new Exception("无法找到指定的文件");
}
oXL = new Excel.Application();
oXL.Visible = false;
oXL.DisplayAlerts = false;
oWB = oXL.Workbooks.Open(Filename: FilePath);
isNew = false;
isOpened = true;
}
///
/// 创建一份excel
///
public void Create()
{
if (oXL != null)
{
throw new Exception("已打开一个Excel文件,请先关闭原文件后再打开新文件");
}
oXL = new Excel.Application();
oXL.Visible = false;
oXL.DisplayAlerts = false;
oWB = oXL.Workbooks.Add();
isNew = true;
isOpened = true;
}
///
/// 获取整个excel表数据
///
///
///
///
public DataTable getSheetAllDatas(int sheetindex = 1, bool firstColsName = true)
{
var oSheet = oWB.Worksheets.get_Item(sheetindex);
int colslen = oSheet.UsedRange.Cells.Columns.Count;//获得已经用行数
int rowslen = oSheet.UsedRange.Cells.Rows.Count;//获得已经用列数
return getSheetDatas(sheetindex, 0, 0, colslen, rowslen, firstColsName);
}
///
/// 获取指定表数据
///
/// 工作薄从1开始
/// 第几列开始,从0开始
/// 第几行开始,从0开始
/// 行数
/// 列数
/// 第一行当成数据列名
public DataTable getSheetDatas(int sheetindex = 1, int colAt = 0, int rowAt = 0, int colslen = 1, int rowslen = 1, bool firstColsName = true)
{
if (oWB == null)
{
throw new Exception("没有打开Excel文件");
}
Excel.Worksheet oSheet = oWB.Worksheets.get_Item(sheetindex);
Excel.Range oRng = oSheet.Range[oSheet.Cells[1 + rowAt, 1 + colAt], oSheet.Cells[rowAt + rowslen, colAt + colslen]];
object[,] celldatas = oRng.Value2;
DataTable dt = new DataTable("ExcelCells");
if (celldatas != null && celldatas.Length > 0)
{
int firstrow = 0;
if (firstColsName)
{
for (int i = 1; i <= celldatas.GetLength(1); i++)
{
var celldata = celldatas[1, i];
if (celldata != null)
{
dt.Columns.Add(celldatas[1, i].ToString());
}
}
firstrow = 2;
}
else
{
for (int i = 1; i <= celldatas.GetLength(1); i++)
{
dt.Columns.Add("col" + i.ToString());
}
firstrow = 1;
}
if (celldatas.GetLength(0) >= firstrow)
{
for (int j = firstrow; j <= celldatas.GetLength(0); j++)
{
var newrow = dt.NewRow();
dt.Rows.Add(newrow);
for (int i = 1; i <= dt.Columns.Count; i++)
{
newrow[i - 1] = celldatas[j, i];
}
}
}
}
return dt;
}
///
/// 具体格写数据
///
///
///
///
///
public void setCell(int sheetindex = 1, int rowIndex = 1, int colIndex = 1, dynamic data = null)
{
Excel.Worksheet oSheet = oWB.Worksheets.get_Item(sheetindex);
oSheet.Cells[rowIndex, colIndex].Value = data;
}
///
/// 具体格读数据
///
///
///
///
///
public string getCell(int sheetindex = 1, int rowIndex = 1, int colIndex = 1)
{
Excel.Worksheet oSheet = oWB.Worksheets.get_Item(sheetindex);
return oSheet.Cells[rowIndex, colIndex].Value;
}
///
/// 指定一个范围的数据导入
///
///
///
///
///
public void setSheetDatas(int sheetindex = 1, int colAt = 0, int rowAt = 0, DataTable dtdatas = null)
{
if (dtdatas != null)
{
Excel.Worksheet oSheet = oWB.Worksheets.get_Item(sheetindex);
object[,] ddatas = new object[dtdatas.Rows.Count, dtdatas.Columns.Count];
for (int i = 0; i < dtdatas.Rows.Count; i++)
{
for (int j = 0; j < dtdatas.Columns.Count; j++)
{
ddatas[i, j] = dtdatas.Rows[i][j];
}
}
Excel.Range oRn = oSheet.Range[oSheet.Cells[rowAt + 1, colAt + 1], oSheet.Cells[rowAt + dtdatas.Rows.Count, colAt + dtdatas.Columns.Count]];
oRn.Value2 = ddatas;
//oRn.Columns.AutoFit();
}
}
///
/// 设置格字符格式
///
///
///
///
///
///
/// 1."@" 数字->文本;2."¥#,###.00" 数字-两位小数;3.考虑excel单元格格式
public void setCellsDataFormat(int sheetindex = 1, int rowbegin = 1, int colbegin = 1, int rowend = 1, int colend = 1, string NumberFormat = null)
{
Excel.Worksheet oSheet = oWB.Worksheets.get_Item(sheetindex);
oSheet.Range[oSheet.Cells[rowbegin, colbegin], oSheet.Cells[rowend, colend]].NumberFormat = NumberFormat;
}
///
/// 设置列格式
///
///
///
/// 1."@" 数字->文本;2."¥#,###.00" 数字-两位小数;3.考虑excel单元格格式
public void setColsDataFormat(int sheetindex = 1, int colIndex = 1, string NumberFormat = null)
{
Excel.Worksheet oSheet = oWB.Worksheets.get_Item(sheetindex);
Excel.Range oRn = oSheet.Columns[colIndex];
oRn.NumberFormat = NumberFormat;
}
///
/// 设置行格式
///
///
///
/// 1."@" 数字->文本;2."¥#,###.00" 数字-两位小数;3.考虑excel单元格格式
public void setRowsDataFormat(int sheetindex = 1, int rowIndex = 1, string NumberFormat = null)
{
Excel.Worksheet oSheet = oWB.Worksheets.get_Item(sheetindex);
Excel.Range oRn = oSheet.Rows[rowIndex];
oRn.NumberFormat = NumberFormat;
}
///
/// 保存excel
///
///
public void Save(ExcelFormat excelformat = ExcelFormat.Excel2000_03)
{
if (string.IsNullOrEmpty(FilePath) || (!isOpened))
{
return;
}
if (isNew)
{
oWB.SaveAs(Filename: FilePath, FileFormat: excelformat);
}
else
{
oWB.Save();
}
}
///
/// 另存excel
///
/// 新路径
///
public void SaveAs(string fullFileName, ExcelFormat excelformat = ExcelFormat.Excel2000_03)
{
if (string.IsNullOrEmpty(fullFileName) || (!isOpened))
{
return;
}
oWB.SaveAs(Filename: fullFileName, FileFormat: excelformat);
}
///
/// 关闭excel.exe
/// 所有对excel操作都应该调用释放内存
///
///
public void Close(bool hasSave = true)
{
if (oWB != null)
{
oWB.Close(hasSave);
CloseComObj(oWB);
}
if (oXL != null)
{
oXL.Workbooks.Close();
oXL.Quit();
CloseComObj(oXL);
}
GC.Collect();
isOpened = false;
}
private string CloseComObj(object obj)
{
string result = "";
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
}
catch (Exception ex)
{
result = ex.Message;
}
finally
{
obj = null;
}
return result;
}
}
}