E
E
Eugene2014-02-21 07:38:59
C++ / C#
Eugene, 2014-02-21 07:38:59

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);

Next, I find the label and replace it with data from the program:
//Очищаем параметры поиска
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);

Everything works great.
Question: how can I deal with the data from the datagridview, do I need to transfer all the data (as I understand it, in a cycle) to one label so that the table expands?
Word or Excel doesn't matter.
PS: Here is an example of a template: 183b565e5d0c43f6a8b79573a4aba622.png
Label $DGV_TABLE$ - where to fill in the data.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2014-02-26
@gloomkolomna

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);
}

D
Daniil Tr, 2014-02-25
@bodrov

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 question

Ask a Question

731 491 924 answers to any question