나는 Excel 파일을 가지고 있는데, A1에서 A27까지 검색하고 '통화'라는 키워드가있는 셀을 찾고 싶습니다.C#의 Excel 범위에서 키워드를 찾는 방법은 무엇입니까?
어떻게하면됩니까?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hExcel = Microsoft.Office.Interop.Excel;
namespace excel004
{
class Program
{
static void Main(string[] args)
{
string xls_path = "c:\\sample.xls";
hExcel.Application xlsApp = new hExcel.ApplicationClass();
hExcel.Workbook xlsWorkbook = xlsApp.Workbooks.Open(
xls_path, //file name
0, //UpdateLinks
true, //read-only
5, //Format
"", //password
"", //write password,
true, //IgnoreReadyOnblyReconnected
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, //Origin
"\t", //Delimiter
false, //Editable
false, //Notify
0, //Converter
true, //AddToMru,
1, //Local
0 //CorruptLoad
);
hExcel.Worksheet xlsWorksheet = (hExcel.Worksheet)xlsWorkbook.Worksheets.get_Item(1);
// Output Worksheet properties
hExcel.Range xlsRange = xlsWorksheet.UsedRange;
int columns = xlsRange.Columns.Count;
int rows = xlsRange.Cells.Rows.Count;
Console.Write("[info]Worksheet columns: "+columns+"\n");
Console.Write("[info]worksheet rows: " + rows + "\n");
// Find Keyword: Currency
//(1)set range
// I stopped here, coz I don't how to search from A1 ~ A27 with keyword:currency
Console.Write(xlsWorksheet.get_Range("A1", "A27").Value2.ToString()); //get nothing
// my excel file is Row:100 * Column:20.
// if the way have to look up all the cells,
// i will give up to ask this question,
// I think it should be a way just look up one column that's A1 to A27.
// [Resolved 1:] Answer from Francesco Baruchelli
hExcel.Range titleRange = xlsWorksheet.get_Range("A1","A27"); //it works
// [Resolved 2:] it works, but not good, coz it get too many unused value
hExcel.Range titleRange = (hExcel.Range)xlsWorksheet.Columns.get_Item(1,Type.Missing);
// why this is wrong?
hExcel.Range titleRange = (hExcel.Range)xlsWorksheet.Columns.get_Item(1,27);
Console.ReadLine();
}
}
}
엑셀 시트에 액세스하고 셀 값을 읽는 데있어 지금까지 시도한 것은 무엇입니까? – Jacob
@cularis, 저는 C#으로 열 수 있습니다. 나는 A1에서 A27까지 범위를 설정하는 방법이 있는지 아십니까? 그리고 나서 각 마셀 값을 마흐 키워드 "통화"로 가져올 수 있습니다. 그러나 MSDN에서 A1에서 A27까지 범위를 설정하는 방법에 대한 중요한 정보는 얻지 못했습니다. – CCC
게시물을 편집하여 소스를 포함 할 수 있습니까? – Jacob