Цитата(CYBERDREAM @ 15.4.2007, 21:11 ) | | У меня офис 2002 года, тобишь 10-й. Какая разница каой офис? |
Ну вообще-то разница минимальная, главное найти длл-ку или OCX/TLB, что там у них поставляется... А потом идёт код типа:
| Код | using System; using System.Collections.Generic; using System.Text; using System.Reflection;
namespace Nota3 { public class WordXPWrapper : IWordOutputWrapper { Word.ApplicationClass _app = null; Word.Document _doc = null;
object optional = Missing.Value; object otrue = true; object ofalse = false;
public WordXPWrapper() { }
#region IWordOutputWrapper Members
bool IWordOutputWrapper.SaveAs(string fileName) { try { _doc.Select(); _app.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceMultiple; _app.Selection.ParagraphFormat.LineSpacing = _app.Selection.ParagraphFormat.LineSpacing * 0.9f;
object ofilename = fileName; _doc.SaveAs(ref ofilename, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional); return true; } catch { return false; } }
#endregion
#region IOutputWrapper Members
#region NewDocument()
AppendTextAlgorythm _algorythm;
bool IOutputWrapper.NewDocument() { try { _app = new Word.ApplicationClass();
object docType = Word.WdDocumentType.wdTypeDocument; _doc = _app.Documents.Add(ref optional, ref optional, ref docType, ref ofalse); _doc.Select(); _doc.PageSetup.TopMargin = 1.5f * 28.3f; _doc.PageSetup.BottomMargin = 2f * 28.3f; _doc.PageSetup.LeftMargin = 3f * 28.3f; _doc.PageSetup.RightMargin = 1.5f * 28.3f; _doc.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
_fmtOptions = FormattingOptions.None; _paragraphMode = ParagraphMode.EachLineFromNewline; _fmtModified = true; _algorythm = new AppendTextAlgorythm();
return true; } catch { return false; } }
#endregion
#region AppendText()
bool _fmtModified = false;
void IOutputWrapper.AppendText(string text, bool fromNewline) { object addedTextStart = _doc.Characters.Count - 1; object addedTextEnd = 0;
string res = text; int diff;
if (!_algorythm.PrepareAppendText(_paragraphMode, ref res, fromNewline, out diff)) return; if (diff != 0) addedTextStart = (int)addedTextStart + diff;
#region Формируем список "команд" (т.е. строк и переносов) List<object> strs = new List<object>(); string laststr = string.Empty; foreach (char ch in res) { if (ch == '\x0d') { if (laststr != string.Empty) strs.Add(laststr); strs.Add(true); laststr = string.Empty; } else if (ch != '\x0a') { laststr += ch; } } if (laststr != string.Empty) strs.Add(laststr); #endregion
#region Добавляем всё в выход foreach (object ob in strs) { if (ob is bool) _app.Selection.TypeParagraph(); else _app.Selection.TypeText((string)ob); } #endregion
#region Форматируем добавленный текст
addedTextEnd = _doc.Characters.Count - 1; if ((int)addedTextEnd < (int)addedTextStart) addedTextStart = (int)addedTextEnd - 1;
switch (_fmtOptions) { case FormattingOptions.None: if (_fmtModified) { Word.Range range = _doc.Range(ref addedTextStart, ref addedTextEnd); Word.Font font = range.Font; font.Bold = 0; font.Size = 11f; font.Scaling = 100; font.Name = "Courier New"; range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify; if ((int)addedTextEnd > (int)addedTextStart) _fmtModified = false; } break; case FormattingOptions.Bold: { Word.Range range = _doc.Range(ref addedTextStart, ref addedTextEnd); Word.Font font = range.Font; font.Bold = 1; font.Size = 11f; font.Scaling = 100; font.Name = "Courier New"; range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify; _fmtModified = true; } break; case FormattingOptions.Center: { Word.Range range = _doc.Range(ref addedTextStart, ref addedTextEnd); Word.Font font = range.Font; font.Bold = 1; font.Size = 11f; font.Scaling = 100; font.Name = "Courier New"; range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; _fmtModified = true; } break; case FormattingOptions.Wide: { Word.Range range = _doc.Range(ref addedTextStart, ref addedTextEnd); Word.Font font = range.Font; font.Size = 14f; font.Bold = 1; font.Scaling = 160; font.Name = "Courier New"; range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; _fmtModified = true; } break; default: throw new NotImplementedException(); }
#endregion }
#endregion
#region CurrentParagraphMode
ParagraphMode _paragraphMode = ParagraphMode.EachLineFromNewline; ParagraphMode IOutputWrapper.CurrentParagraphMode { set { _paragraphMode = value; } get { return _paragraphMode; } }
#endregion
#region CurrentFormattingOptions
FormattingOptions _fmtOptions = FormattingOptions.None; FormattingOptions IOutputWrapper.CurrentFormattingOptions { get { return _fmtOptions; } set { _fmtOptions = value; } }
#endregion
#region AppendTable()
public void AppendTable(Nota3.UI.RodstvoDataSet.RodstvoDataTable table) { IOutputWrapper io = (IOutputWrapper)this; io.CurrentFormattingOptions = FormattingOptions.None; io.AppendText(" ", false);
object rngStart = _doc.Characters.Count - 1; object behavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior; object autofit = Word.WdAutoFitBehavior.wdAutoFitFixed; Word.Range rng = _doc.Range(ref rngStart, ref rngStart); Word.Table wtb = _doc.Tables.Add(rng, table.Rows.Count + 1, table.Columns.Count, ref behavior, ref autofit);
#region Заголовок таблицы Word.Row row0 = wtb.Rows.Item(1); row0.Cells.Item(1).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; _app.Selection.TypeText("№№ п.п."); row0.Cells.Item(2).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; _app.Selection.TypeText("Фамилия, имя, отчество"); row0.Cells.Item(3).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; _app.Selection.TypeText("Степень родства с умершим"); row0.Cells.Item(4).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; _app.Selection.TypeText("Адрес"); #endregion
#region Строки таблицы for (int irow = 0; irow < table.Rows.Count; irow++) { Word.Row wrow = wtb.Rows.Item(irow + 2); Nota3.UI.RodstvoDataSet.RodstvoRow row = table[irow]; wrow.Cells.Item(1).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; _app.Selection.TypeText(row.Index.ToString()); wrow.Cells.Item(2).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; _app.Selection.TypeText(row.FIO); wrow.Cells.Item(3).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; _app.Selection.TypeText(row.Rodstvo); wrow.Cells.Item(4).Select(); _app.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; _app.Selection.TypeText(row.Address); } #endregion
wtb.Columns.Item(1).AutoFit(); wtb.PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent; wtb.PreferredWidth = 80f; wtb.Select();
Word.Range range = _app.Selection.Range; Word.Font font = range.Font; font.Bold = 0; font.Size = 11f; font.Scaling = 100; font.Name = "Courier New";
float pageW = _doc.PageSetup.PageWidth - _doc.PageSetup.RightMargin - _doc.PageSetup.LeftMargin; wtb.Rows.LeftIndent = (pageW * 0.2f) / 2;
rngStart = _doc.Characters.Count - 1; _doc.Range(ref rngStart, ref rngStart).Select();
_fmtModified = true; }
#endregion
#endregion
#region IDisposable Members
void IDisposable.Dispose() { try { _app.Quit(ref otrue, ref optional, ref optional); } catch { } }
#endregion
} } |
|