open and edit multiple .csv files in c# -
my ideal : open .csv files (5 or 6 or more) , add 2 new columns opened files , save it. here code
openfiledialog fopen = new openfiledialog(); fopen.multiselect = true; fopen.filter = "(all type)|*.*"; fopen.showdialog(); if (fopen.filename != null) { excel.application app = new excel.application(); excel.workbook wb = app.workbooks.open(fopen.filename); excel.worksheet sheet = wb.sheets[1]; excel.range range = sheet.usedrange; int column = range.columns.count; int row = range.rows.count; textbox1.text = fopen.filename; //textbox2.text = row.tostring(); //textbox3.text = column.tostring(); range.cells.set_item(1, column + 1, "mo_stmoc"); range.cells.set_item(1, column + 2, "mo_stmoccpl"); (int = 2; <= row; i++) { range.cells.set_item(i, column + 1, "0"); range.cells.set_item(i, column + 2, "0"); } wb.save(); wb.close(); app.workbooks.close(); app.quit(); }
the problem is, when open files , adding 2 columns in first .csv file. i'm new c#, doing wrong here?
you opening multiple files, loop through fopen.filenames instead of using fopen.filename
foreach (string file in fopen.filenames) { //do thing //edit }
additionally better filter csv files instead of all.
fopen.filter = "csv files (*.csv)|*.csv";
Comments
Post a Comment