国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現導入CSV文件到Excel工作簿的方法

2020-01-24 01:40:28
字體:
來源:轉載
供稿:網友

本文實例講述了C#實現導入CSV文件到Excel工作簿的方法。分享給大家供大家參考。具體如下:

你必須在項目中添加對 Microsoft.Office.Core 的引用:from the .NET 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;/// <summary>/// Takes a CSV file and sucks it into the specified worksheet of this workbook at the specified range/// </summary>/// <param name="importFileName">Specifies the full path to the .CSV file to import</param>/// <param name="destinationSheet">Excel.Worksheet object corresponding to the destination worksheet.</param>/// <param name="destinationRange">Excel.Range object specifying the destination cell(s)</param>/// <param name="columnDataTypes">Column data type specifier array. For the QueryTable.TextFileColumnDataTypes property.</param>/// <param name="autoFitColumns">Specifies whether to do an AutoFit on all imported columns.</param>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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚州市| 镇江市| 永善县| 靖江市| 邯郸市| 泊头市| 永昌县| 临沭县| 道真| 新沂市| 怀安县| 高碑店市| 大渡口区| 安阳县| 从江县| 钟山县| 疏附县| 衡南县| 商水县| 定日县| 华亭县| 盐边县| 霍邱县| 皋兰县| 文登市| 石河子市| 隆回县| 武胜县| 扎兰屯市| 房山区| 梅州市| 邵武市| 大方县| 定州市| 江山市| 阜新| 龙门县| 绥阳县| 且末县| 白河县| 丘北县|