CSV、TSV、TXTをExcelに変換する
CSV ファイルを開く
コンマ区切り値(CSV)ファイルには、値がコンマで区切られたレコードが含まれています。データは、各列がコンマ文字で区切られ、二重引用符で引用された表として保存されます。フィールド値に二重引用符文字が含まれる場合は、それを二重引用符文字のペアでエスケープします。Microsoft Excel を使用してスプレッドシートデータを CSV にエクスポートすることもできます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions4 = new LoadOptions(LoadFormat.Csv); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbCSV = new Workbook(dataDir + "Book_CSV.csv", loadOptions4); | |
Console.WriteLine("CSV file opened successfully!"); |
CSV ファイルを開くと無効な文字を置換する
Excel では、特殊文字が含まれる CSV ファイルを開くと、文字が自動的に置換されます。Aspose.Cells API でも同様に行います。これは以下のコード例で示されています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
var filename = sourceDir + "[20180220142533][ASPOSE_CELLS_TEST].csv"; | |
//Load CSV file | |
var workbook = new Workbook(filename, new TxtLoadOptions() { Separator = ';', LoadFilter = new LoadFilter(LoadDataFilterOptions.CellData), CheckExcelRestriction = false, ConvertNumericData = false, ConvertDateTimeData = false }); | |
Console.WriteLine(workbook.Worksheets[0].Name); // (20180220142533)(ASPOSE_CELLS_T | |
Console.WriteLine(workbook.Worksheets[0].Name.Length); // 31 | |
Console.WriteLine("CSV file opened successfully!"); |
好みのパーサーの使用
CSVファイルを開くためにデフォルトのパーサー設定を使用する必要があるわけではないことがあります。場合によっては、CSVファイルをインポートしても日付形式が期待通りでない、または空のフィールドが異なる方法で処理されることがあります。そのために、TxtLoadOptions.PreferredParsersは、異なるデータ型を要件に応じて解析するために独自の好みのパーサーを提供するために利用できます。以下のサンプルコードは、好みのパーサーの使用法を示しています。
この機能をテストするために、サンプルのソースファイルと出力ファイルを以下のリンクからダウンロードできます。
outputsamplePreferredParser.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class TextParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
return value; | |
} | |
public string GetFormat() | |
{ | |
return ""; | |
} | |
} | |
class DateParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
DateTime myDate = DateTime.ParseExact(value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); | |
return myDate; | |
} | |
public string GetFormat() | |
{ | |
return "dd/MM/yyyy"; | |
} | |
} | |
public static void Main() | |
{ | |
// Initialize Text File's LoadFormat | |
LoadFormat oLoadFormat = LoadFormat.Csv; | |
// Initialize Text File's Load options | |
TxtLoadOptions oTxtLoadOptions = new TxtLoadOptions(oLoadFormat); | |
// Specify the separatot character | |
oTxtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding scheme | |
oTxtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Set the flag to true for converting datetime data | |
oTxtLoadOptions.ConvertDateTimeData = true; | |
// Set the preferred parsers | |
oTxtLoadOptions.PreferredParsers = new ICustomParser[] { new TextParser(), new DateParser() }; | |
// Initialize the workbook object by passing CSV file and text load options | |
Workbook oExcelWorkBook = new Aspose.Cells.Workbook(sourceDir + "samplePreferredParser.csv", oTxtLoadOptions); | |
// Get the first cell | |
Cell oCell = oExcelWorkBook.Worksheets[0].Cells["A1"]; | |
// Display type of value | |
Console.WriteLine("A1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Get the second cell | |
oCell = oExcelWorkBook.Worksheets[0].Cells["B1"]; | |
// Display type of value | |
Console.WriteLine("B1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Save the workbook to disc | |
oExcelWorkBook.Save(outputDir + "outputsamplePreferredParser.xlsx"); | |
Console.WriteLine("OpeningCSVFilesWithPreferredParser executed successfully.\r\n"); | |
} |
カスタム区切り記号を使用してテキストファイルを開く
テキストファイルは、書式なしでスプレッドシートデータを保持するために使用されます。この種のファイルは、カスタマイズされた区切り記号を持つプレーンテキストファイルです。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book11.csv"; | |
// Instantiate Text File's LoadOptions | |
TxtLoadOptions txtLoadOptions = new TxtLoadOptions(); | |
// Specify the separator | |
txtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding type | |
txtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath, txtLoadOptions); | |
// Save file | |
wb.Save(dataDir+ "output.txt"); |
タブ区切りファイルを開く
タブ区切り(テキスト)ファイルには、スプレッドシートデータが含まれていますが、フォーマットはありません。データは表やスプレッドシートのように行と列で配置されています。基本的に、タブ区切りファイルは各列の間にタブがある特別な種類のプレーンテキストファイルです。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening Tab Delimited Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions5 = new LoadOptions(LoadFormat.TabDelimited); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbTabDelimited = new Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5); | |
Console.WriteLine("Tab delimited file opened successfully!"); |
タブ区切り値(TSV)ファイルを開く
タブ区切り値(TSV)ファイルには、スプレッドシートデータが含まれていますが、フォーマットはありません。データは表やスプレッドシートのように行と列で配置されています。データはタブ区切りファイルと同様であり、各列の間にタブがあります。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Tsv); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleTSVFile.tsv", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing a cell using its name | |
Cell cell = worksheet.Cells["C3"]; | |
Console.WriteLine("Cell Name: " + cell.Name + " Value: " + cell.StringValue); | |