最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)導(dǎo)入CSV文件到Excel工作簿的方法

 更新時間:2015年06月29日 11:33:32   作者:紅薯  
這篇文章主要介紹了C#實現(xiàn)導(dǎo)入CSV文件到Excel工作簿的方法,涉及C#針對office組件的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了C#實現(xiàn)導(dǎo)入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#程序設(shè)計有所幫助。

相關(guān)文章

  • C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法

    C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法

    這篇文章主要介紹了C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法,本文總結(jié)了Convert.ToDateTime(string)、Convert.ToDateTime(string, IFormatProvider)、DateTime.ParseExact()三種方法,需要的朋友可以參考下
    2015-07-07
  • C#使用Parallel類進(jìn)行多線程編程實例

    C#使用Parallel類進(jìn)行多線程編程實例

    這篇文章主要介紹了C#使用Parallel類進(jìn)行多線程編程的方法,實例分析了Parallel類的相關(guān)使用技巧,需要的朋友可以參考下
    2015-06-06
  • C#中按指定質(zhì)量保存圖片的實例代碼

    C#中按指定質(zhì)量保存圖片的實例代碼

    這篇文章主要介紹了C#中按指定質(zhì)量保存圖片的實例代碼,有需要的朋友可以參考一下
    2013-12-12
  • C# File類中的文件讀寫方法詳解

    C# File類中的文件讀寫方法詳解

    C#提供了多種操作文件的方案,尤其是File類中封裝的靜態(tài)方法,本文將通過一些簡單的示例為大家講講C#讀寫文件的方法,需要的可以參考一下
    2023-05-05
  • c#自定義Attribute獲取接口實現(xiàn)示例代碼

    c#自定義Attribute獲取接口實現(xiàn)示例代碼

    這篇文章主要給大家介紹了關(guān)于c#自定義Attribute獲取接口實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 基于C#實現(xiàn)員工IC卡的讀寫功能

    基于C#實現(xiàn)員工IC卡的讀寫功能

    這篇文章主要為大家詳細(xì)介紹了C#如何實現(xiàn)讀寫員工IC卡的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01
  • C#實現(xiàn)梳排序的使用示例

    C#實現(xiàn)梳排序的使用示例

    梳排序算法是一種改進(jìn)的冒泡排序算法,它通過調(diào)整冒泡排序的間隔來提高排序的效率,本文主要介紹了C#實現(xiàn)梳排序的使用示例,感興趣的可以了解一下
    2023-11-11
  • C#單例類的實現(xiàn)方法

    C#單例類的實現(xiàn)方法

    這篇文章主要介紹了C#單例類的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • C# 中如何利用lambda實現(xiàn)委托事件的掛接

    C# 中如何利用lambda實現(xiàn)委托事件的掛接

    在寫一個小程序的時候,碰到了這樣的問題,需要用委托來掛接事件,但是又想在這事件中使用局部的變量,而委托一旦定義好后,掛接方就沒有辦法再添加額外的形參了。那有沒有什么辦法,可以實現(xiàn)呢
    2013-07-07
  • 一文帶你了解WPF中的附加事件

    一文帶你了解WPF中的附加事件

    附加事件可用于在非元素類中定義新的 路由事件 ,并在樹中的任何元素上引發(fā)該事件。本文通過簡單的示例為大家講解一下WPF附加事件的用法,需要的可以參考一下
    2022-12-12

最新評論

宾阳县| 永州市| 盐池县| 介休市| 江陵县| 隆昌县| 易门县| 通河县| 孝义市| 东丰县| 竹北市| 南澳县| 丹凤县| 龙南县| 沛县| 平湖市| 绩溪县| 高碑店市| 海兴县| 惠安县| 多伦县| 浦北县| 句容市| 方山县| 望城县| 石景山区| 柳江县| 杭锦后旗| 金塔县| 松阳县| 白朗县| 宜阳县| 武乡县| 临沭县| 临洮县| 武清区| 云和县| 嘉鱼县| 绵竹市| 白河县| 太湖县|