
























一.引用Microsoft Excell object Liabrary11
二. /// <summary>
/// 创建excell执行类
/// </summary>
/// <param name="FileName">操作的Excell文件名称</param>
/// <param name="sheetName">操作的表格名称</param>
/// <param name="cellStart">操作的起始单元格</param>
public ExcellDataSet(string FileName, string sheetName)
{
try
{
objApp = new Microsoft.Office.Interop.Excel.Application();
objApp.AskToUpdateLinks = false;
Workbooks objBooks = objApp.Workbooks;
_Workbook objBook = objBooks.Add(FileName);
Sheets objSheets = objBook.Sheets;
_Worksheet objSheet = (_Worksheet)objSheets.get_Item(sheetName);
Range _rang = objSheet.UsedRange;
_rang =
objSheet.get_Range(objSheet.Cells[2, 1], objSheet.Cells[intTotalRowCount, 28]);
_data = (System.Object[,])_rang.Value2;
objBook.Close(false, Missing.Value, Missing.Value);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (objApp != null)
{
objApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);
objApp = null;
}
}
}
三.
/// <summary>
/// EXCELL处理类
/// </summary>
/// <param name="FileName">文件名称</param>
/// <param name="sheetName">表格名称</param>
/// <param name="rowStart">开始行</param>
/// <param name="colStar">开始列</param>
/// <param name="colEnd">结束列</param>
public ExcellDataSet(string FileName, string sheetName, string rowStart, string colStar, string colEnd)
{
try
{
objApp = new Microsoft.Office.Interop.Excel.Application();
Workbooks objBooks = objApp.Workbooks;
_Workbook objBook = objBooks.Add(FileName);
Sheets objSheets = objBook.Sheets;
_Worksheet objSheet = (_Worksheet)objSheets.get_Item(sheetName);
string cellStar = colStar + rowStart;//开始单元格
string rowEnd = objSheet.UsedRange.Cells.Rows.Count.ToString();
string cellEnd = colEnd + rowEnd;//结束单元格
Range _rang = objSheet.get_Range(cellStar, cellEnd);
_data = (System.Object[,])_rang.Value2;
objBook.Close(false, Missing.Value, Missing.Value);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (objApp != null)
{
objApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objApp);
objApp = null;
}
}
}
四.
public object[,] returnObject()
{
if (_data != null)
{
//确定有效数据
int _rowTemp = _data.GetLength(0);
int bbbbb = _data.GetLowerBound(0);
int ccccc = _data.GetUpperBound(0);
int _row = 0;
for (int i = 1; i <= _rowTemp; i++)
{
if (_data.GetValue(i, 2) == null || _data.GetValue(i, 2).ToString().Length == 0)
{
_row = i - 1;
break;
}
}
int _col = _data.GetLength(1);
if (_row == 0)
{
_row = _data.GetLength(0);
}
object[,] _returnData = new object[_row, _col];
int eeee = _returnData.GetLowerBound(0);
int fff = _returnData.GetUpperBound(0);
for (int i = 1; i <= _row; i++)
{
for (int m = 1; m <= _col; m++)
{
_returnData[i - 1, m - 1] = _data[i, m];
}
}
return _returnData;
}
return null;
}
五.
/// <summary>
/// 返回单元格的值
/// </summary>
/// <param name="row">行</param>
/// <param name="col">列(用数字标是)</param>
/// <returns>单元格的值</returns>
public string ReturnCellData(int row, int col)
{
if (_data != null)
{
if (_data.GetValue(row, col) != null)
{
return _data.GetValue(row, col).ToString();
}
}
return string.Empty;
}
/// <summary>
/// 返回单元格的值
/// </summary>
/// <param name="row">行</param>
/// <param name="col">列(用字母标是,必须是大些)</param>
/// <returns>单元格的值</returns>
public string ReturnCellData(int row, string col)
{
char s = char.Parse(col);
int _col = (int)s - 64;
return ReturnCellData(row, _col);
}
///杀掉excel进程
public void KillExcellProcess()
{
int ProceedingCount = 0;
try
{
Process[] ProceddingCon = Process.GetProcesses();
foreach (Process IsProcedding in ProceddingCon)
{
if (IsProcedding.ProcessName.ToUpper() == "EXCEL")
{
ProceedingCount += 1;
IsProcedding.Kill();
}
}
}
catch (System.Exception ex)
{
throw ex;
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。