public docViewerForm(string numberDoc, string dep, string createDate, List <List <String> > data, mainForm.docTypes docType, string pathToTemplate) // для всего, кроме сводного учета { InitializeComponent(); string fileName = makeDocument(docType, numberDoc, dep, createDate, data, pathToTemplate); createdFileName = fileName; showDocument(fileName); }
private string makeDocument(mainForm.docTypes docType, string numberDoc, string dep, string createDate, List <List <string> > data, string pathToTemplate, string docName = null) { // Если docName = null, то к шаблону добавляется текущая дата - это название рапорта. // возвращает путь, по которому сохранен документ DocX document = DocX.Load(pathToTemplate); if (docType == mainForm.docTypes.vedomost) { document.Paragraphs[2].Append(numberDoc).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); document.Paragraphs[3].Append(dep).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); document.Paragraphs[4].Append(createDate).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); } if (docType == mainForm.docTypes.analysis) { document.Paragraphs[1].Append(dep).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); document.Paragraphs[2].Append(createDate).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); document.Paragraphs[3].Append(numberDoc).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); } if (docType == mainForm.docTypes.report) { document.Paragraphs[0].Append(createDate).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); document.Paragraphs[1].Append(numberDoc).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); document.Paragraphs[2].Append(dep).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); } if (docType == mainForm.docTypes.balances) { document.Paragraphs[2].Append(dep).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); document.Paragraphs[3].Append(createDate).FontSize(14). Font("Times New Roman").UnderlineStyle(UnderlineStyle.singleLine).Bold(true); } Table tableinDoc = document.Tables[0]; for (int i = 0; i < data.Count; i++) { tableinDoc.InsertRow(); for (int j = 0; j < data[i].Count; j++) { tableinDoc.Rows[i + 1].Cells[j].Paragraphs[0].Append(data[i][j]).FontSize(14). Font("Times New Roman"); } } if (docName == null) { string path = pathToTemplate.Substring(0, pathToTemplate.Length - 5) + DateTime.Now.ToString().Replace(":", "-") + ".docx"; document.SaveAs(path); document.Dispose(); return(path); } else { document.SaveAs(docName); document.Dispose(); return(docName); } }