Export list from vb.net to excel

anishkt
Contributor
Contributor

Export list from vb.net to excel

anishkt
Contributor
Contributor

Hi,

I have a code in vb.net which inserts blockattributes in a drawing. I want to export the insertion points and the tags for these attributes to excel/csv as x, y and z co-ordinates.

Following is the final list

 

Pnts = (From p As Point3d In blkIpList Order By p.X, p.Y, p.Z Select p).ToList()

 

what is the code for exporting for exporting this to excel csv. I need the XYZ coordinates in separate columns. 

I can take the data from autocad using dataextraction. But since these attributes are arranged along lines, i would not get it for each line.

Can someone please help.

Regards

Anish

0 Likes
Reply
364 Views
2 Replies
Replies (2)

stefan.hofer
Advocate
Advocate

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;
    }
}

 

0 Likes

anishkt
Contributor
Contributor

Thank you for sending the code. I was looking for a VB.NET code. Tried converting to VB.NET. But not working. Especially because the marshal function is not available.

0 Likes