CAD工具之家's Archivers

From boitboy on 2015-06-30 11:01:12

OpenDwg开发实例XlsToDwg

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Teigha.TD; using Teigha.Core; using Aspose.Cells; namespace XlsToDwg { public class XlsToDwgServer { OdDbHostAppServices _hostApp; OdRxSystemServices _sysSrv; private XlsToDwgServer() { _sysSrv = new ExSystemServices(); _hostApp = new CustomServices(); TD_Db.odInitialize(_sysSrv); } private static XlsToDwgServer _Instance = new XlsToDwgServer(); /// <summary> /// 唯一实例 /// </summary> public static XlsToDwgServer Instance { get { return _Instance; } } private void AddIngore(List<KeyValuePair<uint, uint>> ingoreCells, Range range) { for (uint i = 0; i < range.RowCount; i++) { for (uint j = 0; j < range.ColumnCount; j++) { ingoreCells.Add(new KeyValuePair<uint, uint>((uint)(i + range.FirstRow), (uint)(j + range.FirstColumn))); } } } private bool IsIngore(List<KeyValuePair<uint, uint>> ingoreCells, uint i, uint j) { foreach (KeyValuePair<uint, uint> kvp in ingoreCells) { if (kvp.Key == i && kvp.Value == j) { return true; } } return false; } /// <summary> /// 英寸转毫米 /// </summary> /// <param name="inch"></param> /// <returns></returns> private double InchToMM(double inch) { return inch * 25.4; } private void SetTextAlignment(OdDbTable table,uint row,uint column,Cell cell) { } private void TransToTable(OdDbTable table, Cells cells) { table.setNumRows((uint)cells.MaxDataRow + 1); table.setNumColumns((uint)cells.MaxDataColumn + 1); for (uint i = 0; i <= cells.MaxDataRow; i++) { table.setRowHeight(i, InchToMM(cells.GetRowHeightInch((int)i))); } for (uint j = 0; j <= cells.MaxDataColumn; j++) { table.setColumnWidth(j, InchToMM(cells.GetColumnWidthInch((int)j))); } List<KeyValuePair<uint, uint>> ingoreCells = new List<KeyValuePair<uint, uint>>(); for (uint i = 0; i <= cells.MaxDataRow; i++) { for (uint j = 0; j <= cells.MaxDataColumn; j++) { Cell cell = cells.GetCell((int)i, (int)j); if (IsIngore(ingoreCells, i, j)) { continue; } if (cell.IsMerged) { Range range = cell.GetMergedRange(); table.mergeCells(i, (uint)Math.Min(i + range.RowCount - 1, cells.MaxDataRow), j, (uint)Math.Min(j + range.ColumnCount - 1, cells.MaxDataColumn)); AddIngore(ingoreCells, range); } if (cell.Value == null) continue; string text = cell.Value.ToString(); Encoding srcEncoding = Encoding.Unicode; Encoding targetEncoding = Encoding.UTF8; text = targetEncoding.GetString(Encoding.Convert(srcEncoding, targetEncoding, srcEncoding.GetBytes(text))); table.setTextString(i, j, text); table.setTextHeight(i, j, cell.GetStyle().Font.Size / 3.78 / 1.5); TextAlignmentType ha = cell.GetStyle().HorizontalAlignment; TextAlignmentType va = cell.GetStyle().VerticalAlignment; if (ha == TextAlignmentType.Left) { if (va == TextAlignmentType.Bottom) { table.setAlignment(i, j, CellAlignment.kBottomLeft); } else if (va == TextAlignmentType.Center) { table.setAlignment(i, j, CellAlignment.kMiddleLeft); } else if (va == TextAlignmentType.Top) { table.setAlignment(i, j, CellAlignment.kTopLeft); } } else if (ha == TextAlignmentType.Center) { if (va == TextAlignmentType.Bottom) { table.setAlignment(i, j, CellAlignment.kBottomCenter); } else if (va == TextAlignmentType.Center) { table.setAlignment(i, j, CellAlignment.kMiddleCenter); } else if (va == TextAlignmentType.Top) { table.setAlignment(i, j, CellAlignment.kTopCenter); } } else if (ha == TextAlignmentType.Right) { if (va == TextAlignmentType.Bottom) { table.setAlignment(i, j, CellAlignment.kBottomRight); } else if (va == TextAlignmentType.Center) { table.setAlignment(i, j, CellAlignment.kMiddleRight); } else if (va == TextAlignmentType.Top) { table.setAlignment(i, j, CellAlignment.kTopRight); } } } } } OdDbObjectId addStyle(OdDbDatabase pDb, string styleName) { OdDbObjectId styleId; OdDbTextStyleTable pStyles = (OdDbTextStyleTable)pDb.getTextStyleTableId().safeOpenObject(OpenMode.kForWrite); OdDbTextStyleTableRecord pStyle = OdDbTextStyleTableRecord.createObject(); pStyle.setName(styleName); // Add the object to the table. styleId = pStyles.add(pStyle); pStyle.setFileName("txt"); pStyle.setBigFontFileName("gbcbig"); return styleId; } public void Trans(string xlsFile,string dwgFile) { OdDbDatabase odb = _hostApp.createDatabase(true); OdDbObjectId styleId=addStyle(odb, "Font_XlsToDwg"); OdDbDictionary tableDict = (OdDbDictionary)odb.getTableStyleDictionaryId().safeOpenObject(OpenMode.kForWrite); OdDbTableStyle style=OdDbTableStyle.createObject(); style.setTextStyle(styleId); OdDbObjectId tableStyleId=tableDict.setAt("Style_XlsToDwg", style); Workbook book = new Workbook(xlsFile); double xStart = 0F; for (int i = 0; i < book.Worksheets.Count; i++) { Worksheet sheet = book.Worksheets[i]; Cells cells = sheet.Cells; OdDbTable table = OdDbTable.createObject(); table.setTableStyle(tableStyleId); TransToTable(table, cells); OdDbBlockTableRecord pSpace = (OdDbBlockTableRecord)odb.getModelSpaceId().safeOpenObject(OpenMode.kForWrite); table.setPosition(new OdGePoint3d(xStart, 0, 0)); table.generateLayout(); pSpace.appendOdDbEntity(table); double dWidth = table.width(); xStart += dWidth * 1.1; } odb.writeFile(dwgFile, Teigha.TD.SaveType.kDwg, DwgVersion.kDHL_1800); } } }

查看完整版本: OpenDwg开发实例XlsToDwg

Tags: OpenDwg, XlsToDwg


©CAD工具之家
创办于:2013年5月24日