1003 lines
29 KiB
C#
1003 lines
29 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Security.AccessControl;
|
|
using System.Text;
|
|
using System.Windows;
|
|
|
|
using BS.Shared;
|
|
using BS.Shared.Core;
|
|
using BS.Shared.DataContracts;
|
|
using BeWo.Office.Types;
|
|
using BeWo.ServiceProxy;
|
|
using BeWo.View.Detail;
|
|
using BeWo.ViewModel;
|
|
using Microsoft.Office.Interop.Word;
|
|
|
|
using System.IO;
|
|
using Application = Microsoft.Office.Interop.Word.Application;
|
|
using Word = Microsoft.Office.Interop.Word;
|
|
|
|
namespace BeWo.Office
|
|
{
|
|
//struct TCellField
|
|
//{
|
|
// internal int CellIndex;
|
|
// internal String FieldName;
|
|
// internal String Text;
|
|
//}
|
|
|
|
public class WordProcessor
|
|
{
|
|
private Application wordApp;
|
|
private Document wordDoc;
|
|
private String documentPath;
|
|
private ITableModel tableModel;
|
|
private IProgress progress;
|
|
private bool appCreated;
|
|
private bool createFormLetter = true;
|
|
private ViewDestination viewDestination;
|
|
private Dictionary<String, String> usedFieldNames;
|
|
private String tempName;
|
|
private int pictureMergeFieldStart;
|
|
private float placeHolderWidth;
|
|
private float placeHolderHeight;
|
|
private bool removeLineFeed = true;
|
|
private IConverter converter;
|
|
private bool fileSaved;
|
|
public FileAttachmentDC FileAttachment { get; set; }
|
|
private Document tmpDoc;
|
|
public WordDocumentTemplateView TemplateView { get; set; }
|
|
|
|
public String DocumentPath
|
|
{
|
|
get { return documentPath; }
|
|
set { documentPath = value; }
|
|
}
|
|
|
|
public ITableModel TableModel
|
|
{
|
|
get { return this.tableModel; }
|
|
set { this.tableModel = value; }
|
|
}
|
|
|
|
public IProgress Progress
|
|
{
|
|
get { return this.progress; }
|
|
set { this.progress = value; }
|
|
}
|
|
|
|
public bool CreateFormLetter
|
|
{
|
|
get { return this.createFormLetter; }
|
|
set { this.createFormLetter = value; }
|
|
}
|
|
|
|
public bool RemoveLineFeed
|
|
{
|
|
get { return this.removeLineFeed; }
|
|
set { this.removeLineFeed = value; }
|
|
}
|
|
|
|
public bool OptimizePictureSize { get; set; }
|
|
|
|
private void OpenWordApplication()
|
|
{
|
|
appCreated = true;
|
|
wordApp = new Application();
|
|
wordApp.DocumentBeforeSave += wordApp_DocumentBeforeSave;
|
|
var events = (ApplicationEvents4_Event)wordApp;
|
|
events.Quit += Quit;
|
|
}
|
|
|
|
void wordApp_DocumentBeforeSave(Document doc, ref bool saveAsUI, ref bool cancel)
|
|
{
|
|
if (!viewDestination.Equals(ViewDestination.Edit)) return;
|
|
fileSaved = true;
|
|
|
|
tmpDoc = doc;
|
|
}
|
|
|
|
void Quit()
|
|
{
|
|
if (viewDestination.Equals(ViewDestination.Preview))
|
|
{
|
|
File.Delete(documentPath);
|
|
return;
|
|
}
|
|
|
|
if (!viewDestination.Equals(ViewDestination.Edit) || !fileSaved) return;
|
|
|
|
var binaryData = new FileStream(documentPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
|
if (FileAttachment.FileAttachmentOid.HasValue)
|
|
TemplateView.DoFileUpdate(binaryData, Path.GetFileName(documentPath), FileAttachment.Description, FileAttachment.FileAttachmentOid.Value, () => CloseAndDispose(binaryData), (int)FileAttachment.FileAttachmentType);
|
|
|
|
fileSaved = false;
|
|
}
|
|
|
|
public void CloseAndDispose(Stream binaryData)
|
|
{
|
|
binaryData.Close();
|
|
binaryData.Dispose();
|
|
|
|
File.Delete(documentPath);
|
|
}
|
|
|
|
public void CreateReport(ViewDestination destination, IProgress progress, IConverter converter)
|
|
{
|
|
var cancelled = false;
|
|
|
|
try
|
|
{
|
|
viewDestination = destination;
|
|
pictureMergeFieldStart = 0;
|
|
OpenWordApplication();
|
|
this.converter = converter;
|
|
this.progress = progress;
|
|
|
|
if (File.Exists(documentPath))
|
|
{
|
|
if (tableModel != null)
|
|
{
|
|
if (tableModel.Count > 0 && this.progress != null)
|
|
{
|
|
this.progress.Init("Erstelle Bericht", tableModel.Count*2, "Lade Bericht...", true);
|
|
}
|
|
}
|
|
|
|
if (viewDestination == ViewDestination.Edit)
|
|
{
|
|
wordDoc = wordApp.Documents.Open("\"" + documentPath + "\"");
|
|
}
|
|
else
|
|
{
|
|
File.Copy(documentPath, GetWordDocumentTempPath());
|
|
|
|
object missing = Missing.Value;
|
|
wordDoc = wordApp.Documents.Open("\"" + GetWordDocumentTempPath() + "\"",
|
|
ref missing, ref missing, ref missing,
|
|
ref missing, ref missing, ref missing,
|
|
ref missing, ref missing, ref missing,
|
|
ref missing, false);
|
|
}
|
|
if (wordDoc != null)
|
|
{
|
|
wordDoc.Activate();
|
|
}
|
|
|
|
usedFieldNames = GetUsedFieldNames();
|
|
PrepareDocument();
|
|
InsertData(ref cancelled);
|
|
if (cancelled)
|
|
{
|
|
Dispose();
|
|
return;
|
|
}
|
|
|
|
if (this.progress != null)
|
|
{
|
|
if (this.progress.Cancelled)
|
|
{
|
|
Dispose();
|
|
return;
|
|
}
|
|
}
|
|
|
|
switch (destination)
|
|
{
|
|
case ViewDestination.Edit:
|
|
wordApp.Visible = true;
|
|
appCreated = false;
|
|
break;
|
|
|
|
case ViewDestination.Preview:
|
|
CreateDocument(false, destination);
|
|
wordApp.Visible = true;
|
|
appCreated = false;
|
|
break;
|
|
|
|
case ViewDestination.Printer:
|
|
CreateDocument(true, destination);
|
|
wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges);
|
|
|
|
if (Progress != null)
|
|
Progress.Complete();
|
|
return;
|
|
|
|
case ViewDestination.File:
|
|
CreateDocument(false, destination);
|
|
if (this.converter != null)
|
|
{
|
|
this.converter.Convert();
|
|
appCreated = false;
|
|
wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges);
|
|
wordApp.Quit();
|
|
}
|
|
else
|
|
{
|
|
wordDoc.SaveAs();
|
|
}
|
|
break;
|
|
|
|
default:
|
|
Dispose();
|
|
break;
|
|
}
|
|
|
|
if (Progress != null)
|
|
Progress.Complete();
|
|
}
|
|
else
|
|
{
|
|
throw new FileNotFoundException(documentPath);
|
|
}
|
|
|
|
if (destination != ViewDestination.File)
|
|
wordDoc.Saved = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (Progress != null)
|
|
Progress.Complete();
|
|
|
|
Dispose();
|
|
|
|
//#if DEBUG
|
|
// var streamWriter =
|
|
// new StreamWriter(
|
|
// Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WordProcessorErrorLog.txt"), true);
|
|
|
|
// var str = string.Format("{0}\n{1}\n\n", ex.Message, ex.StackTrace);
|
|
|
|
// var exIterator = ex;
|
|
// while (exIterator != null)
|
|
// {
|
|
// str += string.Format("{0}\n{1}\n\n", exIterator.Message, exIterator.StackTrace);
|
|
// var tmpex = exIterator.InnerException;
|
|
// exIterator = tmpex.InnerException;
|
|
// }
|
|
|
|
// streamWriter.WriteLine(str);
|
|
|
|
// streamWriter.Flush();
|
|
// streamWriter.Close();
|
|
//#endif
|
|
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public void EditDocument()
|
|
{
|
|
CreateReport(ViewDestination.Edit, null, null);
|
|
}
|
|
|
|
private void PrepareDocument()
|
|
{
|
|
if (viewDestination != ViewDestination.Edit)
|
|
{
|
|
foreach (MailMergeField field in wordDoc.MailMerge.Fields)
|
|
{
|
|
if (field.Code.Text.ToLower().IndexOf("foto") >= 0)
|
|
{
|
|
field.Select();
|
|
pictureMergeFieldStart = wordApp.Selection.Range.Start;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ReplacePicture(InlineShape shape)
|
|
{
|
|
int endPos = shape.Range.End;
|
|
|
|
if (wordDoc.Range(endPos, endPos + 1).Fields.Count > 0)
|
|
{
|
|
Field mailField = wordDoc.Range(endPos, endPos + 1).Fields[1];
|
|
String code = mailField.Code.Text;
|
|
|
|
if (code.ToLower().IndexOf("foto") >= 0)
|
|
{
|
|
mailField.Select();
|
|
|
|
if (wordApp.Selection.Range.Start == endPos)
|
|
{
|
|
ReplacePictureWithFile(shape, wordApp.Selection.Text);
|
|
mailField.Delete();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ReplacePictureWithFile(InlineShape shape, String fileName)
|
|
{
|
|
float widthPlaceHolder = shape.Width;
|
|
float heightPlaceHolder = shape.Height;
|
|
int pos = shape.Range.Start;
|
|
|
|
shape.Delete();
|
|
|
|
InsertPictureAtPos(fileName, widthPlaceHolder, heightPlaceHolder, pos);
|
|
}
|
|
|
|
private void InsertPictureAtPos(String fileName, float width, float height, int pos)
|
|
{
|
|
if (!String.IsNullOrEmpty(fileName))
|
|
{
|
|
wordApp.Selection.SetRange(pos, pos);
|
|
String filePath = Path.Combine(Path.GetTempPath(), fileName);
|
|
|
|
OfficeManager.AddOpenFile(filePath);
|
|
|
|
if (File.Exists(filePath))
|
|
{
|
|
if (OptimizePictureSize)
|
|
{
|
|
//filePath = DTConvertPicture(lFilePath, PictureConverterPath, aWidth, aHeight, 100)
|
|
//OfficeManager.AddOpenFile(filePath);
|
|
}
|
|
|
|
InlineShape newPicture = wordApp.Selection.InlineShapes.AddPicture(filePath, false, true);
|
|
float widthOrg = newPicture.Width;
|
|
float heightOrg = newPicture.Height;
|
|
|
|
float newWidth;
|
|
float newHeight;
|
|
if ((widthOrg/width) > (heightOrg/height))
|
|
{
|
|
newWidth = width;
|
|
newHeight = heightOrg*(width/widthOrg);
|
|
}
|
|
else
|
|
{
|
|
newHeight = height;
|
|
newWidth = widthOrg*(height/heightOrg);
|
|
}
|
|
|
|
newPicture.Height = newHeight;
|
|
newPicture.Width = newWidth;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CreateDocument(bool print, ViewDestination destination)
|
|
{
|
|
if (CreateFormLetter)
|
|
CreateDocumentWithMailMerge(print, destination);
|
|
else
|
|
CreateDocumentWithTable(print);
|
|
}
|
|
|
|
private void CreateDocumentWithTable(bool print)
|
|
{
|
|
if (Progress != null)
|
|
{
|
|
if (Progress.Cancelled)
|
|
return;
|
|
Progress.SetText("Erstelle Bericht");
|
|
}
|
|
|
|
DeleteUnusedFields();
|
|
|
|
Table templateTable = GetTemplateTable();
|
|
if (templateTable != null)
|
|
{
|
|
int count = tableModel.Count;
|
|
int fieldRangeStart = templateTable.Rows[GetDataRowFromTable(templateTable)].Range.Start;
|
|
|
|
wordApp.Selection.SetRange(fieldRangeStart, fieldRangeStart);
|
|
wordDoc.MailMerge.Fields.AddNext(wordApp.Selection.Range);
|
|
wordApp.Selection.SetRange(fieldRangeStart, templateTable.Range.End);
|
|
|
|
wordApp.Selection.Copy();
|
|
|
|
for (int i = 1; i <= count - 1; i++)
|
|
{
|
|
wordApp.Selection.Paste();
|
|
if (Progress != null)
|
|
{
|
|
if (Progress.Cancelled)
|
|
{
|
|
wordDoc.MailMerge.ViewMailMergeFieldCodes = 0;
|
|
return;
|
|
}
|
|
if (i > count) i = count;
|
|
Progress.ProgressTo(1, "Erstelle Bericht (" + ((i*100)/count) + "%)", false);
|
|
}
|
|
}
|
|
|
|
if (Progress != null)
|
|
Progress.ProgressTo(2, "Erstelle Bericht (100%)", false);
|
|
wordApp.Selection.SetRange(fieldRangeStart, fieldRangeStart + 1);
|
|
wordApp.Selection.Delete();
|
|
}
|
|
|
|
wordDoc.MailMerge.ViewMailMergeFieldCodes = 0;
|
|
|
|
if (Progress != null)
|
|
{
|
|
if (Progress.Cancelled)
|
|
return;
|
|
Progress.SetText("Bilder werden geladen");
|
|
}
|
|
|
|
ReplacePicturesInDocument();
|
|
|
|
if (print)
|
|
{
|
|
wordDoc.PrintOut(true);
|
|
}
|
|
|
|
}
|
|
|
|
private Table GetTemplateTable()
|
|
{
|
|
return wordDoc.Tables.Cast<Table>().FirstOrDefault(TableContainsFieldsInSecondRow);
|
|
}
|
|
|
|
private static bool TableContainsFieldsInSecondRow(Table table)
|
|
{
|
|
return table.Range.Fields.Count > 0 && table.Range.Fields.Cast<Field>().Any(field => field.Type == WdFieldType.wdFieldMergeField);
|
|
}
|
|
|
|
private static int GetDataRowFromTable(Table table)
|
|
{
|
|
if (table.Rows.Count >= 2)
|
|
return 2;
|
|
|
|
return table.Rows.Count == 1 ? 1 : 0;
|
|
}
|
|
|
|
private void ReplacePicturesInDocument()
|
|
{
|
|
if (pictureMergeFieldStart > 0)
|
|
{
|
|
if (wordDoc.InlineShapes.Count > 0)
|
|
{
|
|
List<InlineShape> shapes = wordDoc.InlineShapes.Cast<InlineShape>().ToList();
|
|
|
|
foreach (InlineShape shape in shapes)
|
|
ReplacePicture(shape);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FindPicturePlaceHolder()
|
|
{
|
|
if (pictureMergeFieldStart > 0)
|
|
{
|
|
foreach (InlineShape shape in wordDoc.InlineShapes)
|
|
{
|
|
int endPos = shape.Range.End;
|
|
|
|
foreach (Field field in wordDoc.MailMerge.Fields)
|
|
{
|
|
String code = field.Code.Text;
|
|
if (code.ToLower().IndexOf("foto") >= 0)
|
|
{
|
|
field.Select();
|
|
if (wordApp.Selection.Range.Start == endPos)
|
|
{
|
|
placeHolderWidth = shape.Width;
|
|
placeHolderHeight = shape.Height;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ReplaceMailMergePicture()
|
|
{
|
|
if (pictureMergeFieldStart > 0)
|
|
{
|
|
foreach (InlineShape shape in wordDoc.InlineShapes)
|
|
{
|
|
int endPos = shape.Range.End;
|
|
|
|
foreach (MailMergeField field in wordDoc.MailMerge.Fields)
|
|
{
|
|
String code = field.Code.Text;
|
|
if (code.ToLower().IndexOf("foto") >= 0)
|
|
{
|
|
field.Select();
|
|
|
|
if (wordApp.Selection.Range.Start == endPos)
|
|
{
|
|
shape.Delete();
|
|
|
|
InsertPictureAtPos(wordApp.Selection.Text, placeHolderWidth,
|
|
placeHolderHeight, wordApp.Selection.Range.Start);
|
|
field.Delete();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (Field testField in wordDoc.Fields)
|
|
{
|
|
String code = testField.Code.Text;
|
|
if (code.ToLower().IndexOf("foto") >= 0 &&
|
|
code.ToLower().IndexOf("includepicture") >= 0)
|
|
{
|
|
wordApp.ActiveWindow.View.ShowFieldCodes = true;
|
|
testField.Select();
|
|
|
|
wordApp.Selection.SetRange(wordApp.Selection.Start, wordApp.Selection.Start);
|
|
wordApp.Selection.MoveRight(WdUnits.wdCharacter, 18);
|
|
wordApp.Selection.TypeText(
|
|
"\"C:\\Dokumente und Einstellungen\\christian.BEYONDSOFT\\Lokale Einstellungen\\Temp\\");
|
|
|
|
wordApp.ActiveWindow.View.ShowFieldCodes = false;
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DeleteUnusedFields()
|
|
{
|
|
var deletedFields = new List<MailMergeField>();
|
|
|
|
foreach (MailMergeField field in wordDoc.MailMerge.Fields)
|
|
if (field.Type == WdFieldType.wdFieldMergeField)
|
|
if (!MailMergeFieldExistsInDataSource(field))
|
|
deletedFields.Add(field);
|
|
|
|
foreach (var deletedField in deletedFields)
|
|
deletedField.Delete();
|
|
}
|
|
|
|
private bool MailMergeFieldExistsInDataSource(MailMergeField field)
|
|
{
|
|
String fieldCode = field.Code.Text.Trim();
|
|
String[] array = tableModel.GetFieldNames();
|
|
|
|
if (array != null)
|
|
foreach (var fieldName in array)
|
|
if (fieldCode.IndexOf(" " + fieldName) >= 0 || fieldCode.IndexOf(" \"" + fieldName + "\"") >= 0)
|
|
return true;
|
|
|
|
array = tableModel.GetVariableNames();
|
|
|
|
if (array != null)
|
|
foreach (var fieldName in array)
|
|
if (fieldCode.IndexOf(" " + fieldName) >= 0 || fieldCode.IndexOf(" \"" + fieldName + "\"") >= 0)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
private void CreateDocumentWithMailMerge(bool print, ViewDestination destination)
|
|
{
|
|
if (Progress != null)
|
|
{
|
|
if (Progress.Cancelled)
|
|
return;
|
|
Progress.SetText("Drucke Objekt");
|
|
}
|
|
DeleteUnusedFields();
|
|
|
|
if (pictureMergeFieldStart > 0)
|
|
FindPicturePlaceHolder();
|
|
|
|
wordDoc.MailMerge.DataSource.ActiveRecord = WdMailMergeActiveRecord.wdFirstRecord;
|
|
|
|
for (int i = 1; i <= tableModel.Count; i++)
|
|
{
|
|
if (Progress != null)
|
|
{
|
|
if (Progress.Cancelled)
|
|
return;
|
|
|
|
Progress.ProgressTo(1, "Drucke Objekt " + i + " von " + tableModel.Count, false);
|
|
}
|
|
wordDoc.MailMerge.ViewMailMergeFieldCodes = 0;
|
|
|
|
//Tempbilder löschen
|
|
if (pictureMergeFieldStart > 0)
|
|
{
|
|
//funktioniert nur beim ersten Datensatz
|
|
if (i == 1)
|
|
ReplaceMailMergePicture();
|
|
}
|
|
|
|
if (print)
|
|
{
|
|
wordDoc.Activate();
|
|
wordDoc.PrintOut(i != tableModel.Count);
|
|
}
|
|
|
|
if (destination == ViewDestination.Preview)
|
|
continue;
|
|
wordDoc.MailMerge.DataSource.ActiveRecord = WdMailMergeActiveRecord.wdNextRecord;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (appCreated && wordApp != null)
|
|
wordApp.Quit(WdSaveOptions.wdDoNotSaveChanges);
|
|
}
|
|
|
|
private String GetWordDocumentTempPath()
|
|
{
|
|
if (String.IsNullOrEmpty(tempName))
|
|
tempName = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".doc");
|
|
|
|
return tempName;
|
|
}
|
|
|
|
private String CreateTempPath(String fileName)
|
|
{
|
|
var path = Path.Combine(Path.GetTempPath(), fileName);
|
|
OfficeManager.AddOpenFile(path);
|
|
|
|
return path;
|
|
}
|
|
|
|
private void InsertData(ref bool cancelled)
|
|
{
|
|
wordApp.Options.UpdateFieldsAtPrint = false;
|
|
if (tableModel != null)
|
|
{
|
|
var path = CreateTempPath(Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".txt");
|
|
CreateMailMergeTextFile(path, ref cancelled);
|
|
|
|
if (!cancelled)
|
|
{
|
|
if (viewDestination == ViewDestination.Edit)
|
|
wordApp.Visible = true;
|
|
|
|
wordDoc.MailMerge.OpenDataSource(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CreateMailMergeTextFile(String fileName, ref bool cancelled)
|
|
{
|
|
var fieldNamesText = new StringBuilder();
|
|
var emptyDataFieldsText = new StringBuilder();
|
|
var allFieldNames = GetAllFieldNames();
|
|
|
|
var textStringBuilder = new StringBuilder();
|
|
|
|
foreach (String s in allFieldNames)
|
|
{
|
|
fieldNamesText.Append(s);
|
|
fieldNamesText.Append(Convert.ToChar(59));
|
|
|
|
emptyDataFieldsText.Append("\"");
|
|
emptyDataFieldsText.Append(s);
|
|
emptyDataFieldsText.Append("\"");
|
|
emptyDataFieldsText.Append(Convert.ToChar(59));
|
|
}
|
|
|
|
fieldNamesText.Remove(fieldNamesText.Length - 1, 1);
|
|
emptyDataFieldsText.Remove(emptyDataFieldsText.Length - 1, 1);
|
|
|
|
textStringBuilder.Append(fieldNamesText);
|
|
textStringBuilder.Append("\r\n");
|
|
|
|
tableModel.MoveFirst();
|
|
|
|
int count = 1;
|
|
|
|
if (!tableModel.DataAvailable)
|
|
{
|
|
textStringBuilder.Append(emptyDataFieldsText);
|
|
textStringBuilder.Append("\r\n");
|
|
}
|
|
|
|
while (tableModel.DataAvailable)
|
|
{
|
|
if (Progress != null)
|
|
{
|
|
if (Progress.Cancelled)
|
|
{
|
|
//?Wird im VB Code nicht gesetzt cancelled = true;
|
|
return;
|
|
}
|
|
Progress.ProgressTo(1, "Lade Objekt " + count + " von " + tableModel.Count, false);
|
|
}
|
|
fieldNamesText.Clear();
|
|
foreach (String item in allFieldNames)
|
|
{
|
|
String propertyValue = String.Empty;
|
|
if (usedFieldNames.ContainsKey(item))
|
|
{
|
|
Object obj = tableModel.GetPropertyValue(item);
|
|
if (obj != null)
|
|
propertyValue = obj.ToString();
|
|
}
|
|
if (propertyValue.IndexOf(Convert.ToChar(59)) >= 0)
|
|
propertyValue = propertyValue.Replace(Convert.ToChar(59), ',');
|
|
|
|
if (propertyValue.IndexOf('"') >= 0)
|
|
propertyValue = propertyValue.Replace('"', '\'');
|
|
|
|
//replace "Anführungstriche Oben" Chr(147)
|
|
if (propertyValue.IndexOf(Convert.ToChar(147)) >= 0)
|
|
propertyValue = propertyValue.Replace(Convert.ToChar(147), '-');
|
|
|
|
//replace "Anführungstriche Unten" Chr(132)
|
|
if (propertyValue.IndexOf(Convert.ToChar(132)) >= 0)
|
|
propertyValue = propertyValue.Replace(Convert.ToChar(132), '-');
|
|
|
|
if (RemoveLineFeed)
|
|
{
|
|
if (propertyValue.IndexOf(Convert.ToChar(10)) >= 0)
|
|
propertyValue =
|
|
propertyValue.Replace(String.Format("{0}{1}", Convert.ToChar(13), Convert.ToChar(10)),
|
|
" ");
|
|
}
|
|
fieldNamesText.Append("\"");
|
|
fieldNamesText.Append(propertyValue);
|
|
fieldNamesText.Append("\"");
|
|
fieldNamesText.Append(Convert.ToChar(59));
|
|
}
|
|
fieldNamesText.Remove(fieldNamesText.Length - 1, 1);
|
|
textStringBuilder.Append(fieldNamesText);
|
|
textStringBuilder.Append("\n");
|
|
count++;
|
|
|
|
tableModel.MoveNext();
|
|
}
|
|
|
|
File.WriteAllText(fileName, textStringBuilder.ToString(), Encoding.Default);
|
|
}
|
|
|
|
private Dictionary<String, String> GetUsedFieldNames()
|
|
{
|
|
var fieldNames = new Dictionary<String, String>();
|
|
|
|
var sb = new StringBuilder();
|
|
foreach (Range range in wordDoc.StoryRanges)
|
|
{
|
|
sb.Append(range.Text);
|
|
}
|
|
String text = sb.ToString();
|
|
|
|
int pos = text.IndexOf(Convert.ToChar(171)) + 1;
|
|
while (pos > 0)
|
|
{
|
|
int stop = text.IndexOf(Convert.ToChar(187), pos) + 1;
|
|
|
|
if (stop > 0)
|
|
{
|
|
String fieldName = text.Substring(pos, stop - pos - 1);
|
|
if (!fieldNames.ContainsKey(fieldName))
|
|
fieldNames.Add(fieldName, fieldName);
|
|
pos = text.IndexOf(Convert.ToChar(171), stop) + 1;
|
|
}
|
|
else
|
|
pos = 0;
|
|
}
|
|
|
|
return fieldNames;
|
|
}
|
|
|
|
private String[] GetAllFieldNames()
|
|
{
|
|
var fieldNameDict = new Dictionary<String, bool>();
|
|
var fields = new List<String>();
|
|
|
|
String[] array = tableModel.GetFieldNames();
|
|
if (array != null)
|
|
{
|
|
foreach (string fieldName in array)
|
|
{
|
|
if (!fieldNameDict.ContainsKey(fieldName))
|
|
{
|
|
fields.Add(fieldName);
|
|
fieldNameDict.Add(fieldName, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
array = tableModel.GetVariableNames();
|
|
if (array != null)
|
|
{
|
|
foreach (string fieldName in tableModel.GetVariableNames())
|
|
{
|
|
if (!fieldNameDict.ContainsKey(fieldName))
|
|
{
|
|
fields.Add(fieldName);
|
|
fieldNameDict.Add(fieldName, true);
|
|
}
|
|
}
|
|
}
|
|
return fields.ToArray();
|
|
}
|
|
|
|
/* ~WordProcessor()
|
|
{
|
|
Finalize();
|
|
}
|
|
*/
|
|
/*
|
|
private Dictionary<String, String> GetUsedFieldNamesCSV()
|
|
{
|
|
var selected = new Dictionary<String, String>();
|
|
|
|
String tmpStr = File.ReadAllText(this.documentPath);
|
|
tmpStr = tmpStr.Replace("\"", "");
|
|
tmpStr = tmpStr.Replace("\r\n", "");
|
|
|
|
String[] fields = tmpStr.Split(new String[] { OfficeManager.CSV_DELIMITER }, StringSplitOptions.None);
|
|
|
|
foreach (var fieldName in fields)
|
|
{
|
|
selected.Add(fieldName, fieldName);
|
|
}
|
|
|
|
return selected;
|
|
}
|
|
*/
|
|
/*
|
|
private String GetExcelDocumentTempPath()
|
|
{
|
|
if (String.IsNullOrEmpty(this.tempName))
|
|
this.tempName = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".csv");
|
|
|
|
return this.tempName;
|
|
}
|
|
*/
|
|
/*
|
|
private String EscapeCSVValue(String value)
|
|
{
|
|
String returnValue = value.Replace(Convert.ToChar(147), '-');
|
|
returnValue = returnValue.Replace(Convert.ToChar(132), '-');
|
|
returnValue = returnValue.Replace(Convert.ToChar(13), ' ');
|
|
returnValue = returnValue.Replace(Convert.ToChar(10), ' ');
|
|
returnValue = returnValue.Replace("\"", "\"\"");
|
|
|
|
return "\"" + returnValue + "\"";
|
|
}
|
|
*/
|
|
/*
|
|
private TCellField GetCellFieldFromCell(Cell cell)
|
|
{
|
|
var cellField = new TCellField();
|
|
String cellText = cell.Range.Text;
|
|
int pos = cellText.IndexOf(Convert.ToChar(171));
|
|
|
|
String text = String.Empty;
|
|
|
|
if (pos > 0)
|
|
{
|
|
text = cellText.Substring(0, pos);
|
|
}
|
|
|
|
int stop = 0;
|
|
|
|
bool replaceText = false;
|
|
while (pos >= 0 && stop >= 0)
|
|
{
|
|
stop = cellText.IndexOf(Convert.ToChar(187), pos);
|
|
|
|
if (stop >= 0)
|
|
{
|
|
replaceText = true;
|
|
|
|
String name = cellText.Substring(pos + 1, stop - pos - 1);
|
|
cellField.FieldName = name;
|
|
cellField.Text = text + Convert.ToChar(171) + name + Convert.ToChar(187);
|
|
}
|
|
pos = cellText.IndexOf(Convert.ToChar(171), stop);
|
|
if (pos > stop)
|
|
text = cellText.Substring(stop + 1, pos - stop - 1);
|
|
|
|
}
|
|
|
|
if (replaceText)
|
|
cell.Range.Text = "";
|
|
return cellField;
|
|
}
|
|
*/
|
|
/*
|
|
private String BuildCSVHeaderString()
|
|
{
|
|
var sb = new StringBuilder();
|
|
foreach (var name in usedFieldNames.Values)
|
|
{
|
|
if (sb.Length > 0)
|
|
sb.Append(OfficeManager.CSV_DELIMITER);
|
|
sb.Append(EscapeCSVValue(name));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
*/
|
|
/*
|
|
private String CreateCSVData()
|
|
{
|
|
var sb = new StringBuilder();
|
|
int count = 1;
|
|
|
|
this.tableModel.MoveFirst();
|
|
|
|
while (this.tableModel.DataAvailable)
|
|
{
|
|
if (Progress != null)
|
|
{
|
|
if (Progress.Cancelled)
|
|
return sb.ToString();
|
|
Progress.ProgressTo(1, "Lade Objekt " + count + " von " + this.tableModel.Count, false);
|
|
}
|
|
|
|
|
|
foreach (var field in this.usedFieldNames.Values)
|
|
{
|
|
if (sb.Length > 0)
|
|
sb.Append(OfficeManager.CSV_DELIMITER);
|
|
Object obj = this.tableModel.GetPropertyValue(field);
|
|
if (obj != null)
|
|
sb.Append(EscapeCSVValue(obj.ToString()));
|
|
}
|
|
|
|
sb.Append("\n");
|
|
|
|
count++;
|
|
|
|
this.tableModel.MoveNext();
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
*/
|
|
/*
|
|
private String GetExcelPath()
|
|
{
|
|
// If (mExcelPath = "") Then
|
|
// Dim lExcelApp As Excel.Application
|
|
// Set lExcelApp = New Excel.Application
|
|
// Let mExcelPath = """" & lExcelApp.Path & "\Excel.exe" & """ "
|
|
//End If
|
|
//Let GetExcelPath = mExcelPath
|
|
return null;
|
|
}
|
|
*/
|
|
//Öffnet eine CSV-Datei und liest sie in die aktuelle Tabelle ab Zeile 1 Spalte 1
|
|
//fName = Dateiname & Pfad
|
|
//FS = das Spaltentrennzeichen (Field Seperator), im Deutschen ein Semikolon
|
|
/*
|
|
private void ReadfromCSVSimple(String fname)
|
|
{
|
|
ReadfromCSVSimple(fname, ";");
|
|
}
|
|
*/
|
|
/*
|
|
private void ReadfromCSVSimple(String fname, String FS)
|
|
{
|
|
// Dim hfile As Integer ' Filehandle bzw. Dateinummer
|
|
//Dim i As Long ' Zähler über alle Zeilen
|
|
//Dim j As Integer ' Zähler über alle Spalten
|
|
//Dim OneLine As String ' Eine Zeile als String
|
|
//Dim myArr As Variant ' eine Zeile in Felder getrennt
|
|
|
|
//hfile = FreeFile
|
|
//Open fname For Input As #hfile
|
|
//While Not EOF(hfile)
|
|
// i = i + 1
|
|
// Line Input #hfile, OneLine
|
|
// myArr = Split(OneLine, FS)
|
|
// ' With Range(Cells(i, 1), Cells(i, 1 + UBound(myArr)))
|
|
// ' .NumberFormat = "@"
|
|
// ' .Value = myArr
|
|
// ' End With
|
|
// For j = 0 To UBound(myArr)
|
|
// mExcelApp.Cells(i, 1 + j).NumberFormat = "@"
|
|
// mExcelApp.Cells(i, 1 + j).Value = myArr(j)
|
|
// Next
|
|
//Wend
|
|
//Close #hfile
|
|
}
|
|
*/
|
|
}
|
|
}
|