I am using Microsoft Interop to convert excel files into csv files. I use sheet.SaveAs function.
My initial excel sheet has data from A1 to AZ columns for 100 rows.
I need in the CSV just the data from A1 to AP and only for 50 rows.
Using the Range function, I delete the row51-100, I clear the contents for the same rows, still when I save as CSV, I find rows 51-100 as below: (just commas). I do not want to see these commas in CSV.
,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,
The same for column AQ-AZ as well. I do not want these data in CSV. I delete, clear contents using Range function, yet these AQ-AZ columns appears in CSV files as “,,,,,,,,,,,,,,,,,,,,,” .
Is there a way to save XLS as CSV with only Range that I want to see in the CSV file. Is there a way to control the range that goes into CSV file?
In short, I want to see in CSV file just the data for column A1 to AP for 50 rows. No empty trailing “,”s. Is there a way?
The issue you are describing seems like a "Last Cell" issue. The last cell is the original end of your data, even after you delete rows/columns.
Here is what Microsoft has to say about it: How to reset the last cell in Excel
I seem to remember a programmatic way of doing this, but for the life of me, I cannot recall how.
Having looked at that info, maybe you could rethink how you can do this.
Perhaps you could just read the data you need and write it out yourself.
i.e. For each row in range, get the row as a value which will be an array of object,
convert to array of string, string.join with the delimiter as a comma and append
to a .csv file.
Clearing the contents as suggested in another answer did not work for me, what did work was copying the populated columns in a new worksheet and overwriting the old CSV.
Simply select the trailing empty columns in Excel, right click and select: clear contents. Then save.
Related
I'm reading an .xlsx spreadsheet into a C# console app with a view to outputting the content as a formatted xml file (to be picked up by another part of the system further down the line).
The problem with the the .xslx file is that it's a pro-forma input document based on, and replacing, an old paper-based order form we used to provide to customers, and the input fields aren't organised as a series of similar rows (except in the lower part of the document which consists of up to 99 rows of order detail lines). Some of the rows in the header part of the form/sheet are a mixture of label text AND data; same with the columns.
Effectively, what I need to do is to be able to cherry pick data from the initial dozen or so rows in order to poke data into the xml structure; the latter part of the document I can process by iterating over the rows for the order detail lines.
I can't use Interop as this will end up as an Azure function - so I've used ExcelDataReader to convert the spreadsheet to a dataset, then convert that dataset to a new dataset entirely composed of string values. But I haven't been able to successfully point to individual cells as I had expected to be using syntax something like
var cellValue = MyDataSet.Cell[10, 2];
I'd be grateful for any advice as to how I might get the result I need.
A Dataset has Tables and those have Rows which hold ColumnValues
A WorkSheet transforms into a Table (with Columns) and the Cells transform to Rows and column values.
To find the cell value at [10,2] on the first Worksheet do:
var cellValue = MyDataSet.Tables[0].Rows[10][2];
Remember that cellValue will be of type object. Cast accordingly.
I've 2 excel sheets(Sheet1 & Sheet2) in my Excel workbook. I want to copy row data from Sheet2 to Sheet1.
Condition is:
if Sheet2 copied row doesn't exist in Sheet1 then paste it otherwise don't paste the row.
Copied Rows except 1st row in Sheet2:
Range dataWithoutFirstRow = xlAccrualSheet.Range[xlAccrualSheet.UsedRange.Cells[2, 1],
xlAccrualSheet.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell)];
dataWithoutFirstRow.Copy();
Paste in below used range in Sheet1:
Range DataRange = xlAccrualWorkSheet.Cells[emptycell, 1];
DataRange.PasteSpecial(XlPasteType.xlPasteAllUsingSourceTheme);
Please Tell me How to check already exist rows in Sheet1.
Awaiting for Your Response
Please Tell me How to check already exist rows in Sheet1.
Use Range to read out all data from the workSheet. You are already doing a similar thing accrualSheet. Then, you can use range.Cells(i,j) or range.Rows(r).Value or even range.Rows(r).Cells(i,j) to get the data inside each specific row.
When pasting occurs, loop over all pasted rows, and for each pasted row compare it with rows from workSheet. You may do it directly by reading that on the fly (as mentioned above), or you may read all rows from workSheet and store them in a List and compare incoming rows against that list - it will work much faster that way.
Now, one of the most interesting things is probably what does it mean to "compare rows". Either you will need to compare all cells within a row with another one, or you will need to compare just a specific set of "columns" like "date, time, cause, origin, caseId" etc. But that.. noone knows, you said nothing about that. If there's no info on that, then you probably should compare whole rows and assume that any difference in any cell means that the rows are different.
I have large csv files (from 2GB up to 25GB) which are different structe. I've made a c# app where user enters position of the columns he wants to replace. Now I have to read the csv, compare the columns from csv to columns that are stored in oracle db table, replace the columns from csv if the select conditon is met, and store the result in new csv.
Now the questions are: what is the best and fastest way to do this? What is the best way to access the db only once to do the select statement for column comparissment and not for every csv line?
Now I've used StreamReader for reading and then spliting csv lines, but I don't know what would be the best way to compare it to the values in db?
Thanks for advice.
StreamReader is an easy way which propably also is not that fast.
If you're able to detect the line number without iterating over all lines you may try with MemoryMappedFiles (might also work with streams if they support seeking)
Then jump into the middle of the file and check if you're above or below the desired line.
Then jump to the middle of the remaining half (upper if hit value is below / lower if hit value is above) and repeat.
This converges with a few iterations even on quite large files.
I am trying to read a folder that contain a number of xls files (27) and i need to read only 3 specific columns after 21 row e.g. A21,: B21,: ... In a new column a would like to have just the sum of the previous columns. I am thinking to insert a database grid and to insert there the parsing data. My problem is that i have never try to read something from xls. Do you have any ideas. Thanks in advance! (All the data are in the same workshhet in all workbooks)
A project I am working on requires values from a C# application to update 6 values in an Excel sheet. This excel sheet then populates a named range of items and prices, driven by a number of VLOOKUP formulas and worksheets within the same Excel File.
The two columns in the range are named 'Item' & 'Price'.
'Item' is a column of items concatenated from formulas on a spreadsheet, and these appear fine in the DataTable. However, none of the values in the 'Price' column (all number values from VLOOKUP formulas) appear in the DataTable.
When loading the DataTable in C#, I can see that the two columns' types of string and double are fine. If I replace one of the formula derived values with a hard-coded value the DataTable is fine, so I guess the DataTable itself is working as expected.
If I open the Excel file after the values are passed into it, I can see that the input values went in fine and the formulas have calculated correctly.
So, the only thing I can think is causing the problem is that the formulas are not being recalculated after the values are passed into it.
Is there a way to open an excel file in C# and get it to recalculate all the formulas?
Never mind,
Replaced the OLEDB update with an Excel Interop, opening the Excel file with the Interop allows the formulas to recalculate.