어떻게 Excel 워크 시트를 C#을 사용하여 문자열로 추출 할 수 있습니까? 이미 시트가 있으며 txt 파일로 저장할 수 있지만 그 대신 문자열로 직접 추출하여 일부 처리를 수행하려고합니다.C#에서 문자열로 Excel 워크 시트를 추출하는 중
2
A
답변
1
다음 (안된) 코드 같은 것이 작동합니다 :
Excel.Application oXL= new Excel.Application();
oXL.Visible = true;
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
string s = (string)oSheet.Cells[1, 1].Value;
비록이 sample 보는 등 제대로 모든
을 정리하는 방법을 볼 수
3
화려한 EPPlus 라이브러리를 사용해보십시오.이 라이브러리를 사용하면 스프레드 시트를로드하고 프로그래밍 방식으로 모든 셀에 액세스 할 수 있습니다.
다음은 몇 가지 예입니다 :
이//Select all cells in column d between 9990 and 10000
var query1= (from cell in sheet.Cells["d:d"] where cell.Value is double && (double)cell.Value >= 9990 && (double)cell.Value <= 10000 select cell);
//In combination with the Range.Offset method you can also check values of other columns...
//Here we use more than one column in the where clause.
//We start by searching column D, then use the Offset method to check the value of column C.
var query3 = (from cell in sheet.Cells["d:d"]
where cell.Value is double &&
(double)cell.Value >= 9500 && (double)cell.Value <= 10000 &&
cell.Offset(0, -1).Value is double && //Column C is a double since its not a default date format.
DateTime.FromOADate((double)cell.Offset(0, -1).Value).Year == DateTime.Today.Year+1
select cell);
Console.WriteLine();
Console.WriteLine("Print all cells with a value between 9500 and 10000 in column D and the year of Column C is {0} ...", DateTime.Today.Year + 1);
Console.WriteLine();
count = 0;
//The cells returned here will all be in column D, since that is the address in the indexer.
//Use the Offset method to print any other cells from the same row.
foreach (var cell in query3)
{
Console.WriteLine("Cell {0} has value {1:N0} Date is {2:d}", cell.Address, cell.Value, DateTime.FromOADate((double)cell.Offset(0, -1).Value));
count++;
}
좋아요, 어떤 아이디어를 얻으려면 어떻게 마지막 셀의 셀 번호 또는 행 번호를 얻으십니까? – WhoIsNinja
@ 1100 : [UsedRange] (http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.usedrange (v = vs. 80) .aspx) 속성을 확인하십시오. [범위] (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range_members.aspx)를 제공하는 거기에서 어떤 셀/열이 있는지 알 수 있습니다. 범위. –