C#でExcelを編集することのSource
来源:互联网 发布:摄像头软件下载大全 编辑:程序博客网 时间:2024/05/17 06:26
1.Excel Object を作成時にExcel Fileを開けいました。
2.Dataを 書いて Dataを 読むの方法を作成してしました。
3.RowとColumnについて(Rangeの域)検索するの方法
4.重构
//ExcelHooker.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Excel;
using System.Reflection;
namespace ExcelHooker
...{
public class Program
...{
private Excel.Application oXL;
private Excel._Workbook oWB;
private Excel._Worksheet oSheet;
private Excel.Range oRng;
private Object objMissing = System.Reflection.Missing.Value;
private string path ="C:/Documents and Settings/Administrator/デスクトップ/CreateCodeTool/ExcellHooker/ExcelHooker/ExcelHooker/tmp/";
private string fileName = "Book1.xls";
private string a = "A1", b = "A2";
public Program()
...{
oXL = new Excel.Application();
oXL.Visible = true;
}
private Excel._Workbook OpenFile(string fn)
...{
return (Excel._Workbook)(oXL.Workbooks.Open(path + fileName,objMissing, objMissing, objMissing, objMissing,
objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing));
}
private Excel._Worksheet getSheet(int i, Excel._Workbook oB)
...{
return (Excel._Worksheet)oB.Worksheets.get_Item(i);
}
private Excel.Range getRange(string start, string stop, Excel._Worksheet oS)
...{
return oS.get_Range(start, stop);
}
public void StartExcel()
...{
string tmpStr;
try
...{
//Open the Excel file
System.Console.WriteLine("try to open the excel file!");
oWB = OpenFile(path + fileName);
//Get a sheet in the book
oSheet = getSheet(1, oWB);
//Get a range of data
oRng = getRange(a,b, oSheet);
if (oRng.Value.GetType() == typeof(Object[,]))
...{
Object[,] objd = (System.Object[,])oRng.Value;
tmpStr = objd[1, 1].ToString();
}
else
...{
Object obj = (System.Object)oRng.Value;
tmpStr = obj.ToString();
}
System.Console.WriteLine(tmpStr);
oSheet.get_Range("A1", "D1").Font.Bold = true;
string col = new string("");
string row = new string("");
FindData(oRng, tmpStr, col, row);
}catch(Exception e)...{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, e.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, e.Source);
System.Console.WriteLine(errorMessage, "error");
}
}
public void FindData(Excel.Range range, string target, string col, string row)
...{
string position = FindData(range, target);
string[] str = position.Split('$');
foreach (string s in str)
...{
System.Console.WriteLine(s);
}
}
public string FindData(Excel.Range range, string target)
...{
return Util.Find(range, target);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Excel;
using System.Reflection;
namespace ExcelHooker
...{
public class Program
...{
private Excel.Application oXL;
private Excel._Workbook oWB;
private Excel._Worksheet oSheet;
private Excel.Range oRng;
private Object objMissing = System.Reflection.Missing.Value;
private string path ="C:/Documents and Settings/Administrator/デスクトップ/CreateCodeTool/ExcellHooker/ExcelHooker/ExcelHooker/tmp/";
private string fileName = "Book1.xls";
private string a = "A1", b = "A2";
public Program()
...{
oXL = new Excel.Application();
oXL.Visible = true;
}
private Excel._Workbook OpenFile(string fn)
...{
return (Excel._Workbook)(oXL.Workbooks.Open(path + fileName,objMissing, objMissing, objMissing, objMissing,
objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing));
}
private Excel._Worksheet getSheet(int i, Excel._Workbook oB)
...{
return (Excel._Worksheet)oB.Worksheets.get_Item(i);
}
private Excel.Range getRange(string start, string stop, Excel._Worksheet oS)
...{
return oS.get_Range(start, stop);
}
public void StartExcel()
...{
string tmpStr;
try
...{
//Open the Excel file
System.Console.WriteLine("try to open the excel file!");
oWB = OpenFile(path + fileName);
//Get a sheet in the book
oSheet = getSheet(1, oWB);
//Get a range of data
oRng = getRange(a,b, oSheet);
if (oRng.Value.GetType() == typeof(Object[,]))
...{
Object[,] objd = (System.Object[,])oRng.Value;
tmpStr = objd[1, 1].ToString();
}
else
...{
Object obj = (System.Object)oRng.Value;
tmpStr = obj.ToString();
}
System.Console.WriteLine(tmpStr);
oSheet.get_Range("A1", "D1").Font.Bold = true;
string col = new string("");
string row = new string("");
FindData(oRng, tmpStr, col, row);
}catch(Exception e)...{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, e.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, e.Source);
System.Console.WriteLine(errorMessage, "error");
}
}
public void FindData(Excel.Range range, string target, string col, string row)
...{
string position = FindData(range, target);
string[] str = position.Split('$');
foreach (string s in str)
...{
System.Console.WriteLine(s);
}
}
public string FindData(Excel.Range range, string target)
...{
return Util.Find(range, target);
}
}
}
//Util.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ExcelHooker
...{
public class Util
...{
public static string Find(Excel.Range range, string target)
...{
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Object missing = System.Reflection.Missing.Value;
currentFind = range.Find(target, missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
missing);
while (currentFind != null)
...{
// Keep track of the first range you find.
if (firstFind == null)
...{
firstFind = currentFind;
break;
}
/**///// If you didn't move to a new range, you are done.
//else if (currentFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing)
// == firstFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing))
//{
// break;
//}
//currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
//currentFind.Font.Bold = true;
//currentFind = this.Fruits.FindNext(currentFind);
}
return currentFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ExcelHooker
...{
public class Util
...{
public static string Find(Excel.Range range, string target)
...{
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Object missing = System.Reflection.Missing.Value;
currentFind = range.Find(target, missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
missing);
while (currentFind != null)
...{
// Keep track of the first range you find.
if (firstFind == null)
...{
firstFind = currentFind;
break;
}
/**///// If you didn't move to a new range, you are done.
//else if (currentFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing)
// == firstFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing))
//{
// break;
//}
//currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
//currentFind.Font.Bold = true;
//currentFind = this.Fruits.FindNext(currentFind);
}
return currentFind.get_Address(missing, missing, Excel.XlReferenceStyle.xlA1, missing, missing);
}
}
}
参照:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05152003.asp
- C#でExcelを編集することのSource
- c#&excel
- Importing a Microsoft Excel Source Definition
- Importing a Microsoft Excel Source Definition
- informatica Importing a Microsoft Excel Source Definition 导入EXCEL
- C/C++ source code websites
- C/C++ Source Code Analysis
- Simple C source for CORDIC
- C/C++ source code websites
- Gateway source code -- tcpcli.c
- JavaでExcel文書を操作する一例
- Source Insight:使用Source Insight查看C/C++源代码
- 以Excel 作为Data Source,将data导入db
- Calendar0.01のSource
- RC4加密解密source code(C code)
- Unicode-enabling Microsoft C/C++ Source Code
- Lab 1 Solution C source from internet
- A memery management interface---Source code_except.c
- 测试(一)
- ezStudio,易用高效的业务基础平台开发工具
- 信息系统项目管理师考试初步计划
- 连接
- c++资源之不完全导引(全文)
- C#でExcelを編集することのSource
- JAVA面试题集
- C#中Const类型数据的使用
- delphi的treeview(收集)
- 选oa,如何选平台?
- .Net程序如何防止被注入(整站通用)
- 谈OA中的报表(之一)
- SQLServer和Access、Excel数据传输简单总结
- 关于Sql Server企业管理器MMC 不能打开文件的解决方法