Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[3.142.35.75] |
|
Сообщ.
#1
,
|
|
|
Программа
using System; using System.Configuration; using Word = Microsoft.Office.Interop.Word; using System.Collections.Generic; using System.IO; using System.Configuration; namespace CsWordCons { class Program { static void GetFindString(int numRowBegin, int numCol) { string cyrDir = Environment.CurrentDirectory; string[] strDocFileName = Directory.GetFiles(cyrDir, "*.do*", SearchOption.AllDirectories); if (strDocFileName.Length > 1) { Console.WriteLine("Рядом с программой должен быть один файл с расширением doc или docx!"); Console.ReadKey(); Environment.Exit(1); } Object missing = System.Reflection.Missing.Value; Object confConv = false; Object readOnly = true; Object isVisible = false; Object saveChanges = false; Object filename = strDocFileName[0]; Word.Application word = new Word.Application(); Word.Document doc = new Word.Document(); doc = word.Documents.Open(ref filename, ref confConv, ref readOnly); word.Visible = false; Word.Table tbl = null; try { tbl = doc.Tables[1]; } catch (Exception ex) { Console.WriteLine("В документе " + strDocFileName[0] + " нет таблиц!"); ((Word._Document)doc).Close(ref saveChanges, missing, missing); ((Word._Application)word).Quit(ref saveChanges); Console.ReadKey(); Environment.Exit(1); } int numRows = tbl.Rows.Count; List<string> strListFind = new List<string>(); for (int i = numRowBegin; i <= numRows; i++) { string[] strOnlyFind = tbl.Cell(i, numCol).Range.Text.Split('\r'); foreach (string str in strOnlyFind) { if (str.Length > 2) strListFind.Add(str.Trim()); } } Console.WriteLine("Будет проведен поиск следующих серийных номеров:"); foreach (string str in strListFind) Console.WriteLine(str); Console.WriteLine("---------------------------------------------"); Console.WriteLine("Нажмите любую клавишу для поиска.\nЕсли серийные номера определены неправильно - проверьте\n" + "данные в конфигурационном файле"); Console.ReadKey(); ((Word._Document)doc).Close(ref saveChanges, missing, missing); ((Word._Application)word).Quit(ref saveChanges); } // GetFindString static void Main(string[] args) { Configuration config = ConfigurationManager.OpenExeConfiguration("CsWordCons.exe"); string[] allKeys = config.AppSettings.Settings.AllKeys; int numKeys = allKeys.Length; string strNumRowBegin = config.AppSettings.Settings[allKeys[0]].Value; string strNumCol = config.AppSettings.Settings[allKeys[1]].Value; string strResultFileName = config.AppSettings.Settings[allKeys[numKeys - 1]].Value; GetFindString(int.Parse(strNumRowBegin), int.Parse(strNumCol)); Console.ReadKey(); } // Main } //class Program } // namespace CsWordCons Подскажите, пожалуйста, как: - перемещаться по строкам таблицы; - определить, что в строке объединены ячейки. Проект прикрепил. Прикреплённый файлCsWordTest.zip (56,5 Кбайт, скачиваний: 109) |
Сообщ.
#2
,
|
|
|
string[] strOnlyFind = new string[0]; try { strOnlyFind = tbl.Cell(i, numCol).Range.Text.Split('\r'); } catch (System.Runtime.InteropServices.COMException cex) { if(-2146822347 == cex.ErrorCode) { continue; } throw; } |
Сообщ.
#3
,
|
|
|
А нельзя перемещаться по строкам и определять количество ячеек в строке? Чтобы не создавать исключения. Если можно, то как это сделать?
|
Сообщ.
#4
,
|
|
|
Не знаю.
|
Сообщ.
#5
,
|
|
|
Спасибо за код. Буду использовать исключения.
|