Mehmet Fatih

Mehmet Fatih

  • 855
  • 931
  • 39.8k

Getting grand total in a word document

Jul 1 2023 4:31 PM

I want to include the grand totals of the data I pulled into the word document. However, I couldn't make it. Is there anyone who can help with this? These are my codes.

using System;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
private DataTable GetDataSource()
{
    var dt = new DataTable();
    using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = gezievrak2541.accdb; Jet OLEDB:Database Password = Fatih2541; Mode = ReadWrite"))
    {
        conn.Open();
        using (var adapter = new OleDbDataAdapter("SELECT COUNT (sinifi) as sno,sinifi, SUM(IIF(cinsiyet = 'Kiz', 1, 0)) as kiz,SUM(IIF(cinsiyet = 'Erkek', 1, 0)) as erkek, COUNT(*) as toplam  FROM gezilistemiz25  where sinifi IS NOT NULL GROUP BY  sinifi order BY Left(sinifi,1) desc,Left(sinifi,13) asc,Left(sinifi,12) asc", conn))
        {
            adapter.Fill(dt);
        }
        conn.Close();
        dt.Dispose();
        return dt;
    }
}
private void Sabitbilgi()
{
    DataTable dt = GetDataSource();
    int iTotalFields = 0;
    object oMissing = Missing.Value;
    Word.Application oWord = new Word.Application();
    oWord.Visible = true;
    object oTemplatePath = Application.StartupPath + "\\GeziPlani.docx";
    Word.Document oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
    object Index = "sinifi";
    Word.Table tbl = oWordDoc.Bookmarks.get_Item(ref Index).Range.Tables[1];
    int i = 1; foreach (DataRow r in dt.Rows) r["sno"] = i++; // satir numarasi verir
    foreach (DataRow dr in dt.Rows)
    {
        Word.Row newRow = tbl.Rows.Add(ref oMissing);
        for (int j = 1; j <= dt.Columns.Count; j++)
        {
            newRow.Cells[j].Range.Text = dr[j - 1].ToString();
        }
    }
}


Answers (18)