CHKim

xls to csv format

Apr 23rd, 2016
2,333
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. //css_reference ExcelLibrary.dll
  2. using System;
  3. using System.IO;
  4. using ExcelLibrary.SpreadSheet;
  5.  
  6. /*
  7.  * 엑셀 파일의 각 시트를 csv 포맷으로 바꾸어주는 C# 코드입니다.
  8.  * CS-Script (http://www.csscript.net/) 사용
  9.  * 사용된 dll 라이브러리
  10.  * - ExcelLibrary.dll https://www.nuget.org/packages/ExcelLibrary/
  11.  * 작성자 : CH Kim
  12.  */
  13.  
  14. namespace Xls2Csv
  15. {
  16.     class Xls2Csv
  17.     {
  18.         public static void Main(string[] args)
  19.         {
  20.             if (args.Length != 1)
  21.             {
  22.                 Console.WriteLine("Usage");
  23.                 Console.WriteLine("xls2csv <xls filename>");
  24.                 return;
  25.             }
  26.  
  27.             Xls2Csv app = new Xls2Csv();
  28.             app.convert(args[0]);
  29.         }
  30.  
  31.         private int convert(string filename)
  32.         {
  33.             // gametable.xls 를 읽는다.
  34.             try
  35.             {
  36.                 FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096);
  37.                 Workbook workBook = Workbook.Load(fs);
  38.                 int sheetCount = workBook.Worksheets.Count;
  39.                 if (sheetCount < 1)
  40.                     return 0;
  41.  
  42.                 for (int i = 0; i < sheetCount; ++i)
  43.                 {
  44.                     Worksheet sheet = workBook.Worksheets[i];
  45.  
  46.                     int rowIndex = 1;
  47.                     int lastColIndex = sheet.Cells.LastColIndex;
  48.                     int lastRowIndex = sheet.Cells.LastRowIndex;
  49.  
  50.                     Row row = sheet.Cells.GetRow(rowIndex++);
  51.                     int rowCount = row.LastColIndex + 1;
  52.  
  53.                     string csvFilename = sheet.Name + ".csv";
  54.                     StreamWriter file = new StreamWriter(csvFilename);
  55.  
  56.                     for (int n = 0; n < rowCount; ++n)
  57.                     {
  58.                         string text = row.GetCell(n).ToString();
  59.                         if (n != 0)
  60.                             file.Write(",");
  61.  
  62.                         file.Write(text);
  63.                     }
  64.  
  65.                     file.WriteLine();
  66.  
  67.                     row = sheet.Cells.GetRow(rowIndex++);
  68.  
  69.                     for (int n = 0; n < rowCount; ++n)
  70.                     {
  71.                         string text = row.GetCell(n).ToString();
  72.                         if (n != 0)
  73.                             file.Write(",");
  74.  
  75.                         file.Write(text);
  76.                     }
  77.  
  78.                     file.WriteLine();
  79.  
  80.                     for (int j = rowIndex; j <= lastRowIndex; ++j)
  81.                     {
  82.                         row = sheet.Cells.GetRow(j);
  83.                         if (!row.GetCell(0).IsEmpty)
  84.                         {
  85.                             for (int n = 0; n < rowCount; ++n)
  86.                             {
  87.                                 Cell cell = row.GetCell(n);
  88.                                 string text = cell.ToString();
  89.  
  90.                                 if (n != 0)
  91.                                     file.Write(",");
  92.  
  93.                                 file.Write(text);
  94.                             }
  95.  
  96.                             file.WriteLine();
  97.                         }
  98.                     }
  99.  
  100.                     file.Flush();
  101.                     file.Close();
  102.  
  103.                     System.Console.WriteLine(csvFilename);
  104.  
  105.                 } // end of sheet count
  106.  
  107.                 fs.Close();
  108.                 return sheetCount;
  109.             }
  110.  
  111.             catch (Exception ex)
  112.             {
  113.                 System.Console.WriteLine(ex.ToString());
  114.                 return 0;
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment