Click here to Skip to main content
15,924,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am exporting data to word document table ,table not expanding if records are more than tabla rows in word

What I have tried:

Here is my code

C#
try
{
    SqlDataReader drTemp;
    SqlCommand cmdTemp = new SqlCommand();
    WordDoc = new Word.Application();
    string strFinalCertificateNo = "";
    char[] separators = new char[] { '-', '-' };
    string[] CertificateNo = new string[0];
    CertificateNo = CertificateNum.Split(separators, StringSplitOptions.RemoveEmptyEntries);
    if (CertificateNo.Length > 1)
        strFinalCertificateNo = "PFESC" + "_" + CertificateNo[0].ToString() + "_" + CertificateNo[1].ToString();
    else
        strFinalCertificateNo = "PFESC" + "_" + CertificateNo[0].ToString();

    DateTime dtDate = DateTime.Now.Date;
    string strSource = Application.StartupPath + Path.DirectorySeparatorChar + "App_Certificate" + Path.DirectorySeparatorChar + "PFESC";
    string strDestination = Application.StartupPath + Path.DirectorySeparatorChar + "App_ExportCertificate" + Path.DirectorySeparatorChar;
    if (!Directory.Exists(strDestination))
        Directory.CreateDirectory(strDestination);
  //  strDestination += strFinalCertificateNo + "-" + dtDate.Day + "-" + dtDate.Month + "-" + dtDate.Year + ".docx";
    strDestination += 111 ;

    if (File.Exists(strDestination))
    {
        try
        {
            File.Delete(strDestination);
        }
        catch { }
    }
    object fileName = strSource;
    object readOnly = false;
    object isVisible = true;
    object missing = System.Reflection.Missing.Value;
    WordDoc.Visible = true;
    Doc = WordDoc.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
    Doc.Activate();

    object fileName1 = strSource;
    object readOnly1 = false;
    object isVisible1 = true;
    object missing1 = System.Reflection.Missing.Value;
    WordDoc.Visible = true;
    Doc = WordDoc.Documents.Open(ref fileName1, ref missing1, ref readOnly1, ref missing1, ref missing1, ref missing1,
    ref missing1, ref missing1, ref missing1, ref missing1, ref missing1, ref isVisible1);
    Doc.Activate();


    //Vessel Details
    strQuery = "select InsDate, InsVesselName,InsImoNo,InsClass,InsFlag from InspectionMaster where CompID = '" + strCompanyid +
        "' and InsDeleted = 0 and InsNo = '" + CertificateNum + "'";
    if (cn.State != ConnectionState.Open) cn.Open();
    cmdTemp.Connection = cn;
    cmdTemp.CommandText = strQuery;
    drTemp = cmdTemp.ExecuteReader();
    if (drTemp.Read())
    {
        DateTime dtInsDate = drTemp.GetDateTime(0);
        FindAndReplace(WordDoc, "<vy>", dtInsDate.Year);
        FindAndReplace(WordDoc, "<vm>", dtInsDate.ToString("MMMM"));
        FindAndReplace(WordDoc, "<vd>", dtInsDate.ToString("dd"));
        if (CertificateNum != "")
            FindAndReplace(WordDoc, "<vcertificateno>", CertificateNum);
        else
            FindAndReplace(WordDoc, "<vcertificateno>", "");

        if (drTemp["InsVesselName"] != DBNull.Value)
            FindAndReplace(WordDoc, "<vesselname>", drTemp["InsVesselName"].ToString());
        else
            FindAndReplace(WordDoc, "<vesselname>", "");

        if (drTemp["InsClass"] != DBNull.Value)
            FindAndReplace(WordDoc, "<vclass>", drTemp["InsClass"].ToString());
        else
            FindAndReplace(WordDoc, "<vclass>", "");
        if (drTemp["InsFlag"] != DBNull.Value)
            FindAndReplace(WordDoc, "<vflag>", drTemp["InsFlag"].ToString());
        else
            FindAndReplace(WordDoc, "<vflag>", "");
        if (drTemp["InsImoNo"] != DBNull.Value)
            FindAndReplace(WordDoc, "<vimo>", drTemp["InsImono"].ToString());
        else
            FindAndReplace(WordDoc, "<vimo>", "");

    }
    drTemp.Close();

    // To Export Details Data 
    Word.Table tbl = Doc.Tables[2];
    int intRow = 1, intCol = 2;
    strQuery = "";
    strQuery = "Select SCOrder,SCSerialNo,SCLocation,SCMaker,SCLastServiceDate,SCLastHydroTestDate,SCDType,SCFireClass,SCCapacity," +
               " SCWorkCode from InspPFESCDetails where InsNo = '" + CertificateNum + "' and SCOrder<=25 AND CompID = '" + strCompanyid + "' and SCDeleted  = 0 " +
               " order by SCId";
    if (cn.State != ConnectionState.Open) cn.Open();
    cmdTemp.Connection = cn;
    cmdTemp.CommandText = strQuery;
    drTemp = cmdTemp.ExecuteReader();
    try
    {
        while (drTemp.Read())
        {
            intRow += 1;
            for (intCol = 1; intCol < drTemp.FieldCount; intCol++)
            {
                if (drTemp.GetValue(intCol) != DBNull.Value)
                    tbl.Cell(intRow, intCol + 1).Range.Text = drTemp.GetValue(intCol).ToString();
            }
        }
    }
    catch { }
    drTemp.Close();


    strQuery = "";
    strQuery = "Select SCComments from " + strTechTableName + " where InsNo = '" + CertificateNum + "' and CompID = '" + strCompanyid + "' and SCDeleted  = 0";
    if (cn.State != ConnectionState.Open) cn.Open();
    cmdTemp.Connection = cn;
    cmdTemp.CommandText = strQuery;
    drTemp = cmdTemp.ExecuteReader();
    while (drTemp.Read())
    {
        if (drTemp["SCComments"] != DBNull.Value)
            FindAndReplace(WordDoc, "<sercomment>", drTemp["SCComments"].ToString());
        else
            FindAndReplace(WordDoc, "<sercomment>", "");
    }
    drTemp.Close();
    Doc.SaveAs(strDestination);
}
catch { }
Posted
Updated 20-Feb-17 1:58am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900