검색결과 리스트
CSharp에 해당되는 글 12건
- 2012.09.06 Excel파일 읽기
글
방법1
string strExcelFile = @"c:\test.xslx";
string strConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ strExcelFile
+ ";Extended Properties='Excel 8.0;HDR=YES'" ;
OleDbConnection objExcel=new OleDbConnection(strConnStr);
objExcel.Open();
string strSQL = @"SELECT * FROM[Sheet1$]";
OleDbCommand objCmd = new OleDbCommand(strSQL, objExcel);
OleDbDataobjReader objReader = objCmd.ExecuteobjReader();
StringBuilder sb = new StringBuilder();
while (objReader.Read())
{
for (int i=0; i<objReader.FieldCount; i++)
{
sb.Append(objReader[i].ToString()+",");
}
sb.Append("\n");
}
objExcel.Close();
StreamWriter objWriter = new StreamWriter("trans.txt");
objWriter.Write(sb.ToString());
objWriter.Close();
방법2
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
--------------------------------------------------------------------------------------------------------------------------
Excel.Application xlApp;
Excel.Workbooks xlWorkBooks;
Excel.Workbook xlWorkBook;
Excel.Sheets xlSheet;
Excel.Worksheet xlWorkSheet;
xlApp = new Excel.Application();
xlWorkBooks= xlApp.Workbooks;
String filePath = @"C:\test.xlsx";
xlWorkBook= xlApp.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlSheet = xlWorkBook.Worksheets;
xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;
xlWorkSheet.Cells[1, 1] = "helloworld";
xlRange.PrintOut(1, 1, 1, false, Type.Missing, false, false, Type.Missing);
xlApp.Visible = false;
xlWorkBook.Close(false, null, null);
releaseObject(xlWorkSheet);
releaseObject(xlSheet);
releaseObject(xlWorkBook);
releaseObject(xlWorkBooks);
releaseObject(xlApp);
public static void releaseObject(object obj)//엑셀 메모리 해제
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
throw ex;
}
}
--------------------------------------------------------------------------------------------------------------------------
엑셀 메모리 생성 방식
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(Type.Missing);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
방법3
object mis = Type.Missing;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(@"c:\test.xlsx",mis,mis,mis,mis,mis,mis,mis,mis,mis,mis,mis,mis,mis,mis);
Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets[1] as Excel.Worksheet;
foreach (Excel.Range c in xlWorkSheet.get_Range("a1", "c3").SpecialCells(Excel.XlCellType.xlCellTypeConstants,mis))
{
Console.WriteLine("셀주소는 {0} 이고 값은 {1}입니다.",c.get_Address(false,false, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1,mis,mis), c.Text);
}
xlWorkBook.Close(true, mis, mis);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
참고 : http://blog.naver.com/wzblue?Redirect=Log&logNo=10107209303
참고 : http://cafe.naver.com/vstokorea/55
'-- C#' 카테고리의 다른 글
자동 리디렉션을 너무 많이 시도했습니다. (Too many automatic redirections attempted) (0) | 2012.10.25 |
---|---|
Text파일의 내용을 한줄씩 읽기 (0) | 2012.09.06 |
How to Initialize Hosted WCF Services (0) | 2012.07.18 |
WCF Restful WebService (0) | 2012.06.21 |
Checked, Unchecked (0) | 2011.07.11 |
RECENT COMMENT