Answer the question
In order to leave comments, you need to log in
How to pass data from datagridview to Excel/Word?
Hello.
I have a template in word where labels are written, I open this template:
//создание документа
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
//загрузка документа
Microsoft.Office.Interop.Word.Document doc = null;
string patchto = System.Windows.Forms.Application.StartupPath + "//tmpl//";
object fileName = patchto + "report_client.doc";
object falseValue = false;
object trueValue = true;
object missing = Type.Missing;
doc = app.Documents.Open(ref fileName, ref missing, ref trueValue,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
//Очищаем параметры поиска
app.Selection.Find.ClearFormatting();
app.Selection.Find.Replacement.ClearFormatting();
//Задаём параметры замены и выполняем замену.
object findTextFIO = "$FIO$";
object replaceWithFIO = fio;
object replaceFIO = 2;
app.Selection.Find.Execute(ref findTextFIO, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceWithFIO, ref replaceFIO, ref missing, ref missing, ref missing, ref missing);
Answer the question
In order to leave comments, you need to log in
Decided like this:
xl = new Microsoft.Office.Interop.Excel.Application();
wBook = (Microsoft.Office.Interop.Excel._Workbook)xl.Workbooks.Open(excelTemplatePath, false, false, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
wSheet = (Microsoft.Office.Interop.Excel._Worksheet)wBook.ActiveSheet;
// с какой строки начинаем вставлять данные из dgv
int iRowCount = 11;
for (int i = 0; i < dgv.Rows.Count; i++)
{
wSheet.Cells[iRowCount, 1] = dgv.Rows[i].Cells[0].Value.ToString();
wSheet.Cells[iRowCount, 2] = dgv.Rows[i].Cells[1].Value.ToString();
wSheet.Cells[iRowCount, 3] = dgv.Rows[i].Cells[2].Value.ToString();
wSheet.Cells[iRowCount, 4] = dgv.Rows[i].Cells[3].Value.ToString();
iRowCount++;
// Добавляем строчку ниже
var cellsDRnr = wSheet.get_Range("A" + iRowCount, "A" + iRowCount);
cellsDRnr.EntireRow.Insert(-4121, m_objOpt);
}
Good afternoon.
It will not work to write a table to the label. You can refer directly to a table in a document like this:
Microsoft.Office.Interop.Word.Table table = doc.Tables[1];
Microsoft.Office.Interop.Word.Row row = null;
foreach (string item in collection)
{
row = table.Rows.Add(ref missing);
table.Cell(row.Index, 1/*номер столбца*/).Range.Text = item;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question