/// <summary> /// 这里是否需要try catch呢 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public string Read(string fileName) { _logger.InfoFormat("读取文档 {0} 的内容", fileName); try { if (!_storePolicy.Exist(fileName)) { _logger.ErrorFormat("文档 {0} 不存在", fileName); return(string.Empty); } var bytes = _storePolicy.GetBytes(fileName); using (Stream stream = new MemoryStream(bytes)) { return(_reader.Read(stream, fileName.ToDocumentType())); } } catch (Exception ex) { _logger.Error(ex.Message); _logger.Error(ex.StackTrace); } return(string.Empty); }
public void ReaderTest1() { DummyRender render = new DummyRender(); Document doc = new Document(); doc.LayoutLines.Render = render; doc.Append("a"); DocumentReader reader = doc.CreateReader(); Assert.IsTrue(reader.Read() == 'a'); Assert.IsTrue(reader.Peek() == -1); }
public void ReaderTest2() { DummyRender render = new DummyRender(); Document doc = new Document(); doc.LayoutLines.Render = render; doc.Append("abc"); DocumentReader reader = doc.CreateReader(); char[] buf = new char[2]; int count = reader.Read(buf, 1, 2); Assert.IsTrue(buf[0] == 'b' && buf[1] == 'c'); Assert.IsTrue(count == 2); Assert.IsTrue(reader.Peek() == -1); }
private IEnumerable <Document> StreamDocuments() { var skipped = 0; var took = 0; for (int docId = 0; docId < _ix.DocumentCount; docId++) { var hash = _hashReader.Read(docId); var address = _addressReader.Read(new[] { new BlockInfo(docId * Serializer.SizeOfBlock(), Serializer.SizeOfBlock()) }).First(); var document = _documentReader.Read(new List <BlockInfo> { address }).First(); if (!hash.IsObsolete) { if (skipped == _skip && took < _take) { yield return(document); took++; } else if (skipped < _skip) { skipped++; } else if (took == _take) { break; } } } }
static void Main(string[] args) { // string doc = @"C:\Users\Michael\Dropbox\BFFIS\2017\HoldOversigt-ver1.xls"; string doc = "..\\HoldOversigt-ver1.xls"; if (args.Length != 2) { Console.WriteLine("Usage is bffisApp.exe [fuld sti til excel fil] - e.g. bffisApp.exe c:\\dropbox\\bffis\\HoldOversigt-ver1.xls"); Console.WriteLine("Defaulter til : " + doc); } else { doc = args[1]; } var path = Path.GetDirectoryName(doc); var file = Path.GetFileName(doc); // split into path and filename Directory.SetCurrentDirectory(path); System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); DocumentReader reader = new DocumentReader(); var teams = reader.Read(file); foreach (var t in teams) { Console.Write(t.ToString()); } string year = DateTime.Now.Year + " - " + DateTime.Now.AddYears(1).Year; DocumentBuilder builder = new DocumentBuilder(); builder.Build(teams, "program.pdf", year); builder.BuildSimple(teams, "holdoversigt.pdf", year); }
public void should_read_content_from_word() { string file = Environment.CurrentDirectory + @"\TestFiles\智慧市政综合指挥平台.docx"; string content = _reader.Read(file); Assert.IsTrue(content.Contains("市政")); }
public void Can_read() { var docs = new List <Document> { new Document(0, new List <Field> { new Field("title", "rambo"), new Field("_id", "0") }), new Document(1, new List <Field> { new Field("title", "rocky"), new Field("_id", "1") }), new Document(2, new List <Field> { new Field("title", "rocky 2"), new Field("_id", "2") }) }; var fileName = Path.Combine(CreateDir(), "DocumentReaderTests.Can_read"); var blocks = new Dictionary <int, BlockInfo>(); var keyIndex = docs[0].ToKeyIndex(); var revKeyIndex = keyIndex.ToDictionary(x => x.Value, y => y.Key); using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) using (var writer = new DocumentWriter(fs, Compression.GZip)) { foreach (var doc in docs) { blocks.Add(doc.Id, writer.Write(doc.ToTableRow(keyIndex))); } } using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (var reader = new DocumentReader(fs, Compression.GZip, revKeyIndex)) { var doc = reader.Read(new[] { blocks[2] }); Assert.AreEqual("rocky 2", doc.First().Fields["title"].Value); } using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (var reader = new DocumentReader(fs, Compression.GZip, revKeyIndex)) { var doc = reader.Read(new[] { blocks[1] }); Assert.AreEqual("rocky", doc.First().Fields["title"].Value); } using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (var reader = new DocumentReader(fs, Compression.GZip, revKeyIndex)) { var doc = reader.Read(new[] { blocks[0] }); Assert.AreEqual("rambo", doc.First().Fields["title"].Value); } using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) using (var reader = new DocumentReader(fs, Compression.GZip, revKeyIndex)) { var ds = reader.Read(blocks.Values.OrderBy(b => b.Position).ToList()).ToList(); Assert.AreEqual(3, docs.Count); Assert.IsTrue(ds.Any(d => d.Fields["title"].Value == "rambo")); Assert.IsTrue(ds.Any(d => d.Fields["title"].Value == "rocky")); Assert.IsTrue(ds.Any(d => d.Fields["title"].Value == "rocky 2")); } }
public void Read(Stream stream, bool leaveOpen = false) => DocumentReader.Read(this, stream, leaveOpen);