1

I'm writing a program that copy specific columns from pre-generated .csv file to template xlsx file. With my code I'am only able to achive copying the whole csv file to the first column. Any tips how to make the program see " ; " as a separator during pasting? Also if I overpass this problem how to copy only specific columns that i choose?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace Kreator
{
class Program
{
    static void Main(string[] args)
    {
        Excel.Application srcApp;
        Excel.Workbook srcWorkbook;
        Excel.Worksheet srcWorksheet;


        Excel.Application destApp;
        Excel.Workbook destWorkbook;
        Excel.Worksheet destWorksheet;


        string srcPath = "C:\\Users\\Desktop\\Raport.csv";
        string destPath = "C:\\Users\\Desktop\\XXX.xlsx";



        srcApp = new Excel.Application();
        srcWorkbook = srcApp.Workbooks.Open(srcPath);
        srcWorksheet = srcWorkbook.Worksheets.get_Item(1);




        destApp = new Excel.Application();
        destWorkbook = destApp.Workbooks.Open(destPath,0,false);
        destWorksheet = destWorkbook.Worksheets.get_Item(1);



        Excel.Range srcRange = srcWorksheet.get_Range("A1", "C20");
        Excel.Range destRange = destWorksheet.get_Range("C10","E29");

        srcRange.Copy(Type.Missing);
             destRange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValu            es, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone);

        destWorkbook.SaveAs("C:\\Users\\Desktop\\GotowyPlik" + DateTime.Now.ToString("MM_dd_yyyy") + ".xlsx");


        srcApp.Application.DisplayAlerts = false;
        destApp.Application.DisplayAlerts = false;
        srcWorkbook.Close(false, null, null);
        destWorkbook.Close(true, null, null);
        destApp.Quit();
        srcApp.Quit();

       }
   }
}
Bronchi
  • 21
  • 1
  • 3
  • Use Excel's [TextToColumns](https://msdn.microsoft.com/en-us/library/office/ff193593.aspx) method just after pasting. By the way using [Workbooks.OpenText](https://msdn.microsoft.com/en-us/library/office/ff837097.aspx) you can open a csv inside a workbook without need of copy and paste. – Parfait Oct 25 '15 at 14:19
  • check out here http://stackoverflow.com/questions/16732343/converting-excel-file-from-csv-to-xlsx –  Dec 22 '15 at 17:40

0 Answers0