Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[18.119.213.235] |
|
Сообщ.
#1
,
|
|
|
Visual Studio 2012, Windows 7. В программе
using System; using System.Configuration; using Word = Microsoft.Office.Interop.Word; using System.Collections.Generic; namespace CsWordCons { class Program { static void FindString() { List<string> strArrFind = new List<string> {"111111", "222222", "33333"}; Object missing = System.Reflection.Missing.Value; Object confConv = false; Object readOnly = true; Object isVisible = false; Object saveChanges = false; Word.Application word = new Word.Application(); Word.Document doc = new Word.Document(); word.Visible = false; Word.Table tbl = null; Object filename = Environment.CurrentDirectory + "\\Документ 1.docx"; Console.WriteLine(filename); doc = word.Documents.Open(ref filename, ref confConv, ref readOnly); try { tbl = doc.Tables[1]; } catch (Exception ex) { tbl = null; Console.WriteLine("\nВ документе " + filename.ToString() + " нет таблиц!\n"); Console.ReadKey(); Environment.Exit(1); } int numCols = tbl.Columns.Count, numRows = tbl.Rows.Count; foreach (string strFind in strArrFind) { Console.WriteLine("************* Строка для поиска: {0} *************", strFind); for (int i = 2; i <= numRows; i++) { for (int j = 3; j <= numCols; j++) { Console.WriteLine(tbl.Cell(i, j).Range.Text); Console.WriteLine(tbl.Cell(i, j).Range.Text + " " + strFind); if (tbl.Cell(i, j).Range.Text == strFind) { Console.WriteLine(strFind + " " + i.ToString() + ", " + j.ToString()); } Console.WriteLine("---------------------------------------"); } // for j } // for i } // foreach ((Word._Document)doc).Close(ref saveChanges, missing, missing); ((Word._Application)word).Quit(ref saveChanges); } // FindString() static void Main(string[] args) { FindString(); Console.ReadKey(); } // Main } //class Program } // namespace CsWordCons 1. Вывод в консоль Console.WriteLine(tbl.Cell(i, j).Range.Text + " " + strFind); должен, как я понимаю, вывести текст из ячейки таблицы, затем пробелы, затем поисковую строку. На самом деле пропадает строка tbl.Cell(i, j).Range.Text и выводится только сдвинутая поисковая строка. 2. Условие tbl.Cell(i, j).Range.Text == strFind никогда не выполняется (при установке в теле условия точки прерывания захода в тело условия нет). Помогите, пожалуйста, исправить ошибки. Проект прикрепил. Прикреплённый файлCsWordTest.zip (39,88 Кбайт, скачиваний: 99) |
Сообщ.
#2
,
|
|
|
Там "\r\a" в конце строки.
string cellText = tbl.Cell(i, j).Range.Text; if (cellText.Length >= 2 && "\r\a" == cellText.Substring(cellText.Length - 2, 2)) { cellText = cellText.Substring(0, cellText.Length - 2); } Console.WriteLine(cellText); Console.WriteLine(cellText + " " + strFind); if (cellText == strFind) { Console.WriteLine(strFind + " " + i.ToString() + ", " + j.ToString()); } |
Сообщ.
#3
,
|
|
|
Извините за невнимательность, всё понял
|