Something wrong with your drawing, there are too many invisible texts,
see complete code:
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Interop;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in1.ExcelReport))]
namespace AutoCAD_CSharp_plug_in1
{
class ExcelReport
{
[CommandMethod("xlrep")]
public static void TextSelectToExcel()
{
string title = "";
string jobno = "";
List<string> alltexts = new List<string>();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
string xlFileName = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgprefix");
string dwgname = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgname");
dwgname = dwgname.Substring(0, dwgname.Length - 4) + ".xlsx";
xlFileName = Path.Combine(xlFileName, dwgname);
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 0);
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
PromptPointOptions ppo = new PromptPointOptions("\nSelect a first corner of table: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) return;
PromptCornerOptions pco = new PromptCornerOptions("\nOther corner: ", ppr.Value);
PromptPointResult pcr = ed.GetCorner(pco);
if (pcr.Status != PromptStatus.OK) return;
Point3d p1 = ppr.Value;
Point3d p2 = pcr.Value;
if (p1.X == p2.X || p1.Y == p2.Y)
{
ed.WriteMessage("\nInvalid coordinate specification");
return;
}
TypedValue[] tvs = new TypedValue[]
{new TypedValue(0, "text")
};
SelectionFilter filter = new SelectionFilter(tvs);
PromptSelectionResult result = ed.SelectCrossingWindow(p1, p2, filter);
if (result.Status != PromptStatus.OK) return;
Dictionary<Point3d, string> txtlist = new Dictionary<Point3d, string>();
foreach (SelectedObject selobj in result.Value)
{
DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForRead) as DBObject;
DBText txt = obj as DBText;
if (txt != null)
{
string strtext = txt.TextString;
Point3d pp = txt.Position;
if (!txtlist.ContainsKey(pp))
txtlist.Add(pp, strtext);
}
}
List<Point3d> ptlist = new List<Point3d>(txtlist.Keys);
ptlist.Sort(delegate(Point3d pa, Point3d pb)
{ return (pa.Y.CompareTo(pb.Y)); });
ptlist.Reverse();
title = txtlist[ptlist[6]];
jobno = txtlist[ptlist[5]];
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(string.Format
("Title: {0}\nJob No.: {1}", title, jobno));
//---------------------------------------------------------------------------//
PromptSelectionResult result2 = ed.SelectAll(filter);
if (result2.Status != PromptStatus.OK) return;
foreach (SelectedObject selobj2 in result2.Value)
{
DBObject obj = tr.GetObject(selobj2.ObjectId, OpenMode.ForRead) as DBObject;
DBText txt2 = obj as DBText;
if (txt2 != null)
{
string strtext = txt2.TextString;
if (strtext!=string.Empty)
alltexts.Add(strtext);
}
}
}
//---------------------------------------------------------------------------//
Excel.Application xlApp;
Excel.Workbooks xlBooks;
Excel.Workbook xlBook;
Excel.Sheets xlSheets;
Excel.Worksheet xlSheet;
Excel.Range xlRange;
xlApp = new Excel.Application();
xlApp.Visible = false;
xlApp.SheetsInNewWorkbook = 1;
xlApp.Workbooks.Add(Type.Missing);
xlBooks = (Excel.Workbooks)xlApp.Workbooks;
xlBook = (Excel.Workbook)xlBooks[1];
xlBook.Saved = true;
xlApp.DisplayAlerts = false;
xlSheets = (Excel.Sheets)xlBook.Worksheets;
xlSheet = (Excel.Worksheet)xlSheets.get_Item(1);
xlRange = (Excel.Range)xlSheet.Range["A1"];
xlRange.ColumnWidth = 20;
xlRange.NumberFormat = "@";
int row = 1;
xlRange.Cells[row, 1] = title;
row = 1;
xlRange = (Excel.Range)xlSheet.Range["B1"];
xlRange.ColumnWidth = 20;
xlRange.NumberFormat = "@";
xlRange.Cells[row, 1] = jobno;
xlRange = (Excel.Range)xlSheet.Range["A2"];
int r = 1;
foreach (string s in alltexts)
{
xlRange.Cells[r, 1] = s;
r += 1;
}
//Save workbook
xlBook.SaveAs(xlFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp.Workbooks.Close();
xlApp.Quit();
//or
// xlApp.Windows[1].Close(false, Type.Missing, Type.Missing);
}
catch (System.Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"\n{0}", ex.Message);
}
finally
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Remove blank rows from Excel file manually");
}
}
}
}
~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919