Something like this?
//using excel = Microsoft.Office.Interop.Excel;
public static void myExample()
{
List<Point3d> myPoints = new List<Point3d>();
// xlsx or xls
string fileformat = "xlsx";
string filename = "myExcelFilename";
bool done = CreateExcelSheetExample("C:\\" + filename + "." + fileformat, fileformat, myPoints);
if (!done) MessageBox.Show("Something went wrong...");
}
public static bool CreateExcelSheetExample(string filename, string format, List<Point3d> myPoints)
{
excel.Application oex = new excel.Application();
object misValue = System.Reflection.Missing.Value;
if (oex == null)
{
_ = System.Windows.MessageBox.Show("Excel is not properly installed!");
return false;
}
// Create the workbook
excel.Workbook owb = oex.Workbooks.Add(misValue);
// Get the sheet ( 1 = first sheet )
excel.Worksheet ows = (excel.Worksheet)owb.Worksheets.get_Item(1);
// Format the first column ( A ) as text
//try
//{
// excel.Range chartrange = (excel.Range)ows.Cells[1, 1];
// chartrange.EntireColumn.NumberFormat = "@";
//}
//catch (System.Exception ex)
//{
// MessageBox.Show("Cell format error. Original error: " + ex.Message);
//}
// Cell values
for (int row = 0; row < myPoints.Count(); row++)
{
ows.Cells[row, 1] = myPoints[row].X; // column A
ows.Cells[row, 2] = myPoints[row].Y; // column B
ows.Cells[row, 3] = myPoints[row].Z; // column C
}
// Write the file
try
{
try
{
if (format == "xlsx")
{
// .xlsx
owb.SaveAs(filename, excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, excel.XlSaveAsAccessMode.xlNoChange, misValue, misValue, misValue, misValue, misValue);
}
else
{
// .xls
owb.SaveAs(filename, excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
}
owb.Close(true, misValue, misValue);
oex.Quit();
}
catch
{
owb.Close(false, misValue, misValue);
oex.Quit();
_ = Marshal.ReleaseComObject(ows);
_ = Marshal.ReleaseComObject(owb);
_ = Marshal.ReleaseComObject(oex);
return false;
}
_ = Marshal.ReleaseComObject(ows);
_ = Marshal.ReleaseComObject(owb);
_ = Marshal.ReleaseComObject(oex);
return true;
}
catch
{
_ = Marshal.ReleaseComObject(ows);
_ = Marshal.ReleaseComObject(owb);
_ = Marshal.ReleaseComObject(oex);
return false;
}
}