Reputation: 1
I have been reading up on ways to read a CSV File in C# and I have heard that it is really complicated. What I am looking to do is have a console read a csv and if there are cells in column A that contain 123 it will delete all rows with a cell in column A with 123. BUT, as it does this all.. I need it to save to ANOTHER excel file so I have the original file as well. This is what I have to read the file.
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string str;
int rCnt = 0;
int cCnt = 0;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("123abc.csv", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Upvotes: 0
Views: 1648
Reputation: 836
Reading is a bit more simple you can use: https://code.google.com/p/linqtoexcel/ and that makes it dead easy.
Writing back is a bit more complicated. If you can use the interop for excel. [1]http://msdn.microsoft.com/en-us/library/e4x1k99a.aspx
or
[2] Open XML SDK 2.0 http://msdn.microsoft.com/en-us/library/bb448854(office.14).aspx
After your edit, why not make a copy of the original, then edit and save the copy? Since you are already using Excel.ApplicationClass(), you can also use xlApp.Save("filename")
Here is how you can find things in excel: Excel.Range.Find method
Upvotes: 0
Reputation: 19544
Have a look into either the EPPlus library - You could use that to read in the Excel sheet and then do your manipulation fairly easily. Or use things such as Linq to Excel. There are lots of questions / info about that already on SO, or look at this introductory video. Or you could query the sheet as a DataBase if it's in tabular format.
Hope this helps!!
Upvotes: 1