public static ShapefileDataWriter Create(string path, DbfHeader dbfHeader, ShapefileHeader shapeHeader) { ShapefileDataWriter writer = new ShapefileDataWriter(path, shapeHeader, FileMode.CreateNew, FileAccess.Write); writer._writerShape.BaseStream.Seek(100L, SeekOrigin.Begin); writer._writerIndex.BaseStream.Seek(100L, SeekOrigin.Begin); writer._dbf = DbfFile.Create(Path.ChangeExtension(path, ".dbf"), dbfHeader); writer._currentRecord = new DbfRecord(dbfHeader); writer._recordNumber = 1; writer._filePos = 50; return(writer); }
public string CreateDbfFile(RegistryInfo registryInfo) { try { var workDirectory = Path.Combine(Directory.GetCurrentDirectory(), @"Abonents\" + registryInfo.Now.ToString("dd.MM.yyyy")); var dbfFilePath = Path.Combine(workDirectory, $"{registryInfo.FileName}" + ".dbf"); if (!Directory.Exists(workDirectory)) { Directory.CreateDirectory(workDirectory); } if (File.Exists(dbfFilePath)) { File.Delete(dbfFilePath); } var dbfFile = new DbfFile(); dbfFile.Create(dbfFilePath); dbfFile.Header.Unlock(); dbfFile.Header.AddColumn("FIO", DbfColumn.DbfColumnType.Character, 100, 0); dbfFile.Header.AddColumn("ACCOUNT", DbfColumn.DbfColumnType.Character, 20, 0); var record = new DbfRecord(dbfFile.Header, Encoding.GetEncoding(1251)) { }; foreach (Abonent row in registryInfo.Abonents) { record[0] = row.Fio; record[1] = row.Account; dbfFile.Write(record, true); } dbfFile.Close(); return(dbfFilePath); } catch (Exception ex) { throw new Exception($"Error in CreateDbfFile: {ex}"); } }
/// <summary> /// writes content of data queue into output DBF file /// </summary> /// <param name="queue"></param> protected override void DoWrite(IDataQueue queue) { var dbf = new DbfFile(); var formatter = new BedValueFormatter(FormatSettings); try { if (File.Exists(GetWorkingFileName())) { File.Delete(GetWorkingFileName()); } dbf.Create(GetWorkingFileName()); ITableStructure ts = queue.GetRowFormat; foreach (var col in ts.Columns) { DbfColumn.DbfColumnType type; int len = 0, scale = 0; switch (col.DataType.Code) { case DbTypeCode.Array: case DbTypeCode.Generic: case DbTypeCode.Text: case DbTypeCode.Xml: type = DbfColumn.DbfColumnType.Memo; break; case DbTypeCode.Blob: type = DbfColumn.DbfColumnType.Binary; break; case DbTypeCode.Datetime: var dtype = (DbTypeDatetime)col.DataType; if (dtype.SubType == DbDatetimeSubType.Date) { type = DbfColumn.DbfColumnType.Date; } else { type = DbfColumn.DbfColumnType.Character; len = DateTime.UtcNow.ToString("s").Length; } break; case DbTypeCode.Float: type = DbfColumn.DbfColumnType.Number; len = 18; scale = DefaultNumericScale; break; case DbTypeCode.Int: if (AllowFoxProInteger) { type = DbfColumn.DbfColumnType.Integer; } else { type = DbfColumn.DbfColumnType.Number; len = 18; } break; case DbTypeCode.Logical: type = DbfColumn.DbfColumnType.Boolean; break; case DbTypeCode.Numeric: type = DbfColumn.DbfColumnType.Number; len = 18; scale = ((DbTypeNumeric)col.DataType).Scale; break; case DbTypeCode.String: var stype = (DbTypeString)col.DataType; if (stype.IsBinary) { type = DbfColumn.DbfColumnType.Binary; } else if (stype.Length <= 254) { type = DbfColumn.DbfColumnType.Character; len = stype.Length; if (len <= 0) { len = DefaultStringLength; } } else { type = DbfColumn.DbfColumnType.Memo; } break; default: type = DbfColumn.DbfColumnType.Character; len = DefaultStringLength; break; } dbf.Header.AddColumn(col.ColumnName, type, len, scale); } var orec = new DbfRecord(dbf.Header); while (!queue.IsEof) { var record = queue.GetRecord(); orec.Clear(); for (int i = 0; i < ts.Columns.Count; i++) { record.ReadValue(i); formatter.ReadFrom(record); orec[i] = formatter.GetText(); } dbf.Write(orec); } } finally { dbf.Close(); queue.CloseReading(); } FinalizeBulkCopy(); }