C#操作Excel的几个函数
2011-01-19 17:21
441 查看
//获得某个文本的在excel的sheet中的位置
public Position GetPosition(Worksheet mySheet,string strText)
{
Position positon = new Position();
try
{
Range currentFind = mySheet.Cells.Find(strText, Type.Missing,
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
Type.Missing, Type.Missing);
if (currentFind!=null)
{
positon.Row = currentFind.Row;
positon.Column = currentFind.Column;
}
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message + " please contact the administrator!");
}
return positon;
}
//获得位置,给定区域内获取
public Position GetPosition(Worksheet mySheet, string strText,int MaxRow,int MinRow,int Column,decimal Amount)
{
Position positon = new Position();
bool bFind = false;
bool bComplete = false;
try
{
if (strText=="")
{
return positon;
}
Range currentFind = mySheet.Cells.Find(strText, Type.Missing,
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
Type.Missing, Type.Missing);
if (currentFind != null)
{
if ((currentFind.Row >= (MinRow + 1)) && (currentFind.Row <= MaxRow) && (currentFind.Column == (Column + 1)))
{
Range range = (Range)mySheet.Cells[currentFind.Row, currentFind.Column + 2];
string strResult = "";
if (range.Value2 != null)
{
strResult = range.Value2.ToString();
}
decimal currentAmount = decimal.Parse(strResult);
if (currentAmount == Amount)
{
bFind = true;
bComplete = true;
positon.Row = currentFind.Row;
positon.Column = currentFind.Column;
}
}
}
else
{
bComplete = true;
}
//continue to find
Range rangeFind = currentFind;
while (!bComplete)
{
if (!bFind)
{
Range nextFind = mySheet.Cells.FindNext(rangeFind);
if (nextFind != null)
{
if (!((nextFind.Row == currentFind.Row) && (nextFind.Column == currentFind.Column)))
{
rangeFind = nextFind;
if ((nextFind.Row >= (MinRow + 1)) && (nextFind.Row < MaxRow) && (nextFind.Column == (Column + 1)))
{
Range range = (Range)mySheet.Cells[nextFind.Row, nextFind.Column + 2];
string strResult = "";
if (range.Value2 != null)
{
strResult = range.Value2.ToString();
}
decimal currentAmount = decimal.Parse(strResult);
if (currentAmount == Amount)
{
bFind = true;
bComplete = true;
positon.Row = nextFind.Row;
positon.Column = nextFind.Column;
}
}
}
else
{
bComplete = true;
}
}
else
{
bComplete = true;
}
}
}
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message + " please contact the administrator!");
}
return positon;
}
//插入行
if (iRowNeedInsert > 0)
{
//insert blank rows
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)mySheet1.Rows[pSDTotal1.Row - 1, missing];
for (int i = 0; i < iRowNeedInsert; i++)
{
range.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, missing);
}
}
//复制和粘贴
rCopy1 = "G" + (pSD2.Row + 1).ToString() + " ";
rCopy2 = "J" + (pSDLast2.Row).ToString() + " ";
rPaste = "G" + (pSDLast1.Row + 1).ToString() + " ";
mySheet2.get_Range(rCopy1, rCopy2).Copy(System.Type.Missing);
mySheet1.get_Range(rPaste, System.Type.Missing).PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteAll, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, System.Type.Missing, System.Type.Missing);
//置为空
mySheet1.get_Range(mySheet1.Cells[iCountLC, pLC1.Column], mySheet1.Cells[iCountLC, pLC1.Column + 4]).Value2 = "";
//排序
Microsoft.Office.Interop.Excel.Range rngLC = mySheet1.get_Range(mySheet1.Cells[pLC1.Row + 1, pLC1.Column], mySheet1.Cells[pLCLast1.Row, pLC1.Column + 4]);
rngLC.Sort(rngLC, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
missing, missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);
public Position GetPosition(Worksheet mySheet,string strText)
{
Position positon = new Position();
try
{
Range currentFind = mySheet.Cells.Find(strText, Type.Missing,
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
Type.Missing, Type.Missing);
if (currentFind!=null)
{
positon.Row = currentFind.Row;
positon.Column = currentFind.Column;
}
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message + " please contact the administrator!");
}
return positon;
}
//获得位置,给定区域内获取
public Position GetPosition(Worksheet mySheet, string strText,int MaxRow,int MinRow,int Column,decimal Amount)
{
Position positon = new Position();
bool bFind = false;
bool bComplete = false;
try
{
if (strText=="")
{
return positon;
}
Range currentFind = mySheet.Cells.Find(strText, Type.Missing,
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
Type.Missing, Type.Missing);
if (currentFind != null)
{
if ((currentFind.Row >= (MinRow + 1)) && (currentFind.Row <= MaxRow) && (currentFind.Column == (Column + 1)))
{
Range range = (Range)mySheet.Cells[currentFind.Row, currentFind.Column + 2];
string strResult = "";
if (range.Value2 != null)
{
strResult = range.Value2.ToString();
}
decimal currentAmount = decimal.Parse(strResult);
if (currentAmount == Amount)
{
bFind = true;
bComplete = true;
positon.Row = currentFind.Row;
positon.Column = currentFind.Column;
}
}
}
else
{
bComplete = true;
}
//continue to find
Range rangeFind = currentFind;
while (!bComplete)
{
if (!bFind)
{
Range nextFind = mySheet.Cells.FindNext(rangeFind);
if (nextFind != null)
{
if (!((nextFind.Row == currentFind.Row) && (nextFind.Column == currentFind.Column)))
{
rangeFind = nextFind;
if ((nextFind.Row >= (MinRow + 1)) && (nextFind.Row < MaxRow) && (nextFind.Column == (Column + 1)))
{
Range range = (Range)mySheet.Cells[nextFind.Row, nextFind.Column + 2];
string strResult = "";
if (range.Value2 != null)
{
strResult = range.Value2.ToString();
}
decimal currentAmount = decimal.Parse(strResult);
if (currentAmount == Amount)
{
bFind = true;
bComplete = true;
positon.Row = nextFind.Row;
positon.Column = nextFind.Column;
}
}
}
else
{
bComplete = true;
}
}
else
{
bComplete = true;
}
}
}
}
catch (Exception e)
{
MessageBox.Show("Error:" + e.Message + " please contact the administrator!");
}
return positon;
}
//插入行
if (iRowNeedInsert > 0)
{
//insert blank rows
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)mySheet1.Rows[pSDTotal1.Row - 1, missing];
for (int i = 0; i < iRowNeedInsert; i++)
{
range.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, missing);
}
}
//复制和粘贴
rCopy1 = "G" + (pSD2.Row + 1).ToString() + " ";
rCopy2 = "J" + (pSDLast2.Row).ToString() + " ";
rPaste = "G" + (pSDLast1.Row + 1).ToString() + " ";
mySheet2.get_Range(rCopy1, rCopy2).Copy(System.Type.Missing);
mySheet1.get_Range(rPaste, System.Type.Missing).PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteAll, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, System.Type.Missing, System.Type.Missing);
//置为空
mySheet1.get_Range(mySheet1.Cells[iCountLC, pLC1.Column], mySheet1.Cells[iCountLC, pLC1.Column + 4]).Value2 = "";
//排序
Microsoft.Office.Interop.Excel.Range rngLC = mySheet1.get_Range(mySheet1.Cells[pLC1.Row + 1, pLC1.Column], mySheet1.Cells[pLCLast1.Row, pLC1.Column + 4]);
rngLC.Sort(rngLC, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
missing, missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);
相关文章推荐
- C#操作Excel的几个函数
- C#操作Excel的函数
- TroubleShoot:C#操作Excel的几个问题
- C#操作Excel的函数
- c#操作excel后关闭excel.exe的方法
- C#的office操作[word&amp;Excel]
- 使用C#和Excel进行报表开发(二)-操作统计图(Chart)
- c#操作Excel
- Asp.net/c#+OleDb操作excel文件(二),数据传输使用Parameters
- c#.net 下的excel操作(二)
- C# 操作Excel的类
- C语言面试必须会的几个字符串操作函数原型strcpy,strcmp,strlen,strcat
- C# 操作数据导出到Excel遇到的一个小问题
- [转]C#操作Excel开发报表系列整理
- 在窗体中操作EXCEL(C#)
- C#对Excel的相关操作
- C# 操作Excel之旁门左道 [ C# | Excel ] .
- C#操作Excel(导入导出)
- C#操作Excel文件~创建Excel文件