C# CSV 파일을 Excel 워크북으로 가져오는 방법

3675 단어
이 예제에서는 C#을 사용하여 CSV 파일을 Excel 워크북으로 가져오는 방법을 설명합니다.여러분에게 참고할 수 있도록 나누어 드리겠습니다.구체적으로 다음과 같습니다.
프로젝트에 Microsoft를 추가해야 합니다.Office.코어의 인용:from theNET tab of the Visual Studio Add Reference dialog box, and the Microsoft Excel 12.0 Object Library (you can use 14.0 if you want, too, but nothing lower).
C# 코드는 다음과 같습니다.

using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
/// 
/// Takes a CSV file and sucks it into the specified worksheet of this workbook at the specified range
/// 
/// Specifies the full path to the .CSV file to import
/// Excel.Worksheet object corresponding to the destination worksheet.
/// Excel.Range object specifying the destination cell(s)
/// Column data type specifier array. For the QueryTable.TextFileColumnDataTypes property.
/// Specifies whether to do an AutoFit on all imported columns.
public void ImportCSV(string importFileName, Excel.Worksheet destinationSheet,
  Excel.Range destinationRange, int[] columnDataTypes, bool autoFitColumns)
{
  destinationSheet.QueryTables.Add(
    "TEXT;" + Path.GetFullPath(importFileName),
  destinationRange, Type.Missing);
  destinationSheet.QueryTables[1].Name = Path.GetFileNameWithoutExtension(importFileName);
  destinationSheet.QueryTables[1].FieldNames = true;
  destinationSheet.QueryTables[1].RowNumbers = false;
  destinationSheet.QueryTables[1].FillAdjacentFormulas = false;
  destinationSheet.QueryTables[1].PreserveFormatting = true;
  destinationSheet.QueryTables[1].RefreshOnFileOpen = false;
  destinationSheet.QueryTables[1].RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells;
  destinationSheet.QueryTables[1].SavePassword = false;
  destinationSheet.QueryTables[1].SaveData = true;
  destinationSheet.QueryTables[1].AdjustColumnWidth = true;
  destinationSheet.QueryTables[1].RefreshPeriod = 0;
  destinationSheet.QueryTables[1].TextFilePromptOnRefresh = false;
  destinationSheet.QueryTables[1].TextFilePlatform = 437;
  destinationSheet.QueryTables[1].TextFileStartRow = 1;
  destinationSheet.QueryTables[1].TextFileParseType = XlTextParsingType.xlDelimited;
  destinationSheet.QueryTables[1].TextFileTextQualifier = XlTextQualifier.xlTextQualifierDoubleQuote;
  destinationSheet.QueryTables[1].TextFileConsecutiveDelimiter = false;
  destinationSheet.QueryTables[1].TextFileTabDelimiter = false;
  destinationSheet.QueryTables[1].TextFileSemicolonDelimiter = false;
  destinationSheet.QueryTables[1].TextFileCommaDelimiter = true;
  destinationSheet.QueryTables[1].TextFileSpaceDelimiter = false;
  destinationSheet.QueryTables[1].TextFileColumnDataTypes = columnDataTypes;
  Logger.GetInstance().WriteLog("Importing data...");
  destinationSheet.QueryTables[1].Refresh(false);
  if (autoFitColumns==true)
    destinationSheet.QueryTables[1].Destination.EntireColumn.AutoFit();
  // cleanup
  this.ActiveSheet.QueryTables[1].Delete();
}


사용 방법은 다음과 같습니다.

myOwnWorkbookClass.ImportCSV(
   @"C:\MyStuff\MyFile.CSV",
   (Excel.Worksheet)(MyWorkbook.Worksheets[1]),
   (Excel.Range)(((Excel.Worksheet)MyWorkbook.Worksheets[1]).get_Range("$A$7")),
   new int[] { 2, 2, 2, 2, 2 }, true);


본 논문이 여러분의 C#프로그래밍에 도움이 되기를 바랍니다.

좋은 웹페이지 즐겨찾기