简体   繁体   中英

C# Opening csv string in Excel without saving the string

I have a string in csv format, that I wish to simply open in Excel, without first having to save the file.

Is this possible? I've had a look at Interop but I cannot find the specific method that I need.

Thanks.

If you convert your csv-string to a 2-dimensional array first, you can pass it to a range of cells.

Excel.Range oRange = oSheet.Range("A1",Missing.Value);
oRange.Resize(myArray.GetLength(0),myArray.GetLength(1));
oRange.Value = myArray;

This code is written out of my mind, so I hope It's ok, but I think you get the picture. If you like to include a header in your excel-file, just start the range from A2 instaed of A1.

Yes, just copy the values over manually. You can find a sample here .

Adapting that sample to get a very short sample to get you started (please note, this sample is not complete and you need to read the complete sample to see how to do things properly):

Excel.Application oXL= new Excel.Application();
oXL.Visible = true;
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
oSheet.Cells[1, 1] = [first value from csv];

But assuming that it's a range of values in the csv string, just do a String.Split on it and then use the array to populate a Range in the sheet, just check out the sample in the article for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM