Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Jan 19, 2016 7:16 am

I have an online school management system that can generate a report card. So what this is supposed to do is when I click "Generate Card," I should be able to view the card opened in Microsoft Word. Here's my code:

Code: Select all
Document document = new Document();

        //load a document
        document.LoadFromFile(@"h:\root\home\btslpc-001\www\site1\CardTemplate.doc");

        //Replace text
        document.Replace("<studentno>", this.ddlStudent.SelectedValue, true, true);
        document.Replace("<name>", this.ddlStudent.SelectedItem.Text, true, true);
        document.Replace("<levelsection>", this.lblSection.Text, true, true);
        document.Replace("<sy>", this.lblSchoolYear.Text, true, true);
        document.Replace("<levelsection>", this.lblSection.Text, true, true);
        clsMain main = new clsMain();
        main.Username = Session["Username"].ToString();

        if (main.GetFullName())
        {
           
            document.Replace("<adviser>", main.FullName.ToString(), true, true);
           
        }
        else
        {
           
        }

        document.Replace("(Teacher)", "", true, true);

        Label subject1 = (Label)GridView1.Rows[0].Cells[1].FindControl("Label1");
        Label subject2 = (Label)GridView1.Rows[1].Cells[1].FindControl("Label1");
        Label subject3 = (Label)GridView1.Rows[2].Cells[1].FindControl("Label1");
        Label subject4 = (Label)GridView1.Rows[3].Cells[1].FindControl("Label1");
        Label subject5 = (Label)GridView1.Rows[4].Cells[1].FindControl("Label1");
        Label subject6 = (Label)GridView1.Rows[5].Cells[1].FindControl("Label1");
        Label subject7 = (Label)GridView1.Rows[6].Cells[1].FindControl("Label1");
        Label subject8 = (Label)GridView1.Rows[7].Cells[1].FindControl("Label1");
        Label subject9 = (Label)GridView1.Rows[8].Cells[1].FindControl("Label1");
        Label subject10 = (Label)GridView1.Rows[9].Cells[1].FindControl("Label1");
        Label subject11 = (Label)GridView1.Rows[10].Cells[1].FindControl("Label1");

        TextBox q1 = (TextBox)GridView1.Rows[0].Cells[2].FindControl("txt1stquarter");
        TextBox q2 = (TextBox)GridView1.Rows[1].Cells[2].FindControl("txt1stquarter");
        TextBox q3 = (TextBox)GridView1.Rows[2].Cells[2].FindControl("txt1stquarter");
        TextBox q4 = (TextBox)GridView1.Rows[3].Cells[2].FindControl("txt1stquarter");
        TextBox q5 = (TextBox)GridView1.Rows[4].Cells[2].FindControl("txt1stquarter");
        TextBox q6 = (TextBox)GridView1.Rows[5].Cells[2].FindControl("txt1stquarter");
        TextBox q7 = (TextBox)GridView1.Rows[6].Cells[2].FindControl("txt1stquarter");
        TextBox q8 = (TextBox)GridView1.Rows[7].Cells[2].FindControl("txt1stquarter");
        TextBox q9 = (TextBox)GridView1.Rows[8].Cells[2].FindControl("txt1stquarter");
        TextBox q10 = (TextBox)GridView1.Rows[9].Cells[2].FindControl("txt1stquarter");
        TextBox q11 = (TextBox)GridView1.Rows[10].Cells[2].FindControl("txt1stquarter");

        TextBox w1 = (TextBox)GridView1.Rows[0].Cells[3].FindControl("txt2ndquarter");
        TextBox w2 = (TextBox)GridView1.Rows[1].Cells[3].FindControl("txt2ndquarter");
        TextBox w3 = (TextBox)GridView1.Rows[2].Cells[3].FindControl("txt2ndquarter");
        TextBox w4 = (TextBox)GridView1.Rows[3].Cells[3].FindControl("txt2ndquarter");
        TextBox w5 = (TextBox)GridView1.Rows[4].Cells[3].FindControl("txt2ndquarter");
        TextBox w6 = (TextBox)GridView1.Rows[5].Cells[3].FindControl("txt2ndquarter");
        TextBox w7 = (TextBox)GridView1.Rows[6].Cells[3].FindControl("txt2ndquarter");
        TextBox w8 = (TextBox)GridView1.Rows[7].Cells[3].FindControl("txt2ndquarter");
        TextBox w9 = (TextBox)GridView1.Rows[8].Cells[3].FindControl("txt2ndquarter");
        TextBox w10 = (TextBox)GridView1.Rows[9].Cells[3].FindControl("txt2ndquarter");
        TextBox w11 = (TextBox)GridView1.Rows[10].Cells[3].FindControl("txt2ndquarter");

        TextBox e1 = (TextBox)GridView1.Rows[0].Cells[4].FindControl("txt3rdquarter");
        TextBox e2 = (TextBox)GridView1.Rows[1].Cells[4].FindControl("txt3rdquarter");
        TextBox e3 = (TextBox)GridView1.Rows[2].Cells[4].FindControl("txt3rdquarter");
        TextBox e4 = (TextBox)GridView1.Rows[3].Cells[4].FindControl("txt3rdquarter");
        TextBox e5 = (TextBox)GridView1.Rows[4].Cells[4].FindControl("txt3rdquarter");
        TextBox e6 = (TextBox)GridView1.Rows[5].Cells[4].FindControl("txt3rdquarter");
        TextBox e7 = (TextBox)GridView1.Rows[6].Cells[4].FindControl("txt3rdquarter");
        TextBox e8 = (TextBox)GridView1.Rows[7].Cells[4].FindControl("txt3rdquarter");
        TextBox e9 = (TextBox)GridView1.Rows[8].Cells[4].FindControl("txt3rdquarter");
        TextBox e10 = (TextBox)GridView1.Rows[9].Cells[4].FindControl("txt3rdquarter");
        TextBox e11 = (TextBox)GridView1.Rows[10].Cells[4].FindControl("txt3rdquarter");

        TextBox f1 = (TextBox)GridView1.Rows[0].Cells[5].FindControl("txt3rdquarter");
        TextBox f2 = (TextBox)GridView1.Rows[1].Cells[5].FindControl("txt3rdquarter");
        TextBox f3 = (TextBox)GridView1.Rows[2].Cells[5].FindControl("txt3rdquarter");
        TextBox f4 = (TextBox)GridView1.Rows[3].Cells[5].FindControl("txt3rdquarter");
        TextBox f5 = (TextBox)GridView1.Rows[4].Cells[5].FindControl("txt3rdquarter");
        TextBox f6 = (TextBox)GridView1.Rows[5].Cells[5].FindControl("txt3rdquarter");
        TextBox f7 = (TextBox)GridView1.Rows[6].Cells[5].FindControl("txt3rdquarter");
        TextBox f8 = (TextBox)GridView1.Rows[7].Cells[5].FindControl("txt3rdquarter");
        TextBox f9 = (TextBox)GridView1.Rows[8].Cells[5].FindControl("txt3rdquarter");
        TextBox f10 = (TextBox)GridView1.Rows[9].Cells[5].FindControl("txt3rdquarter");
        TextBox f11 = (TextBox)GridView1.Rows[10].Cells[5].FindControl("txt3rdquarter");
       
       

        document.Replace("<subject1>", subject1.Text, true, true);
        document.Replace("<subject2>", subject2.Text, true, true);
        document.Replace("<subject3>", subject3.Text, true, true);
        document.Replace("<subject4>", subject4.Text, true, true);
        document.Replace("<subject5>", subject5.Text, true, true);
        document.Replace("<subject6>", subject6.Text, true, true);
        document.Replace("<subject7>", subject7.Text, true, true);
        document.Replace("<subject8>", subject8.Text, true, true);
        document.Replace("<subject9>", subject9.Text, true, true);
        document.Replace("<subject10>", subject10.Text, true, true);
        document.Replace("<subject11>", subject11.Text, true, true);

        document.Replace("Q1", q1.Text, true, true);
        document.Replace("Q2", q2.Text, true, true);
        document.Replace("Q3", q3.Text, true, true);
        document.Replace("Q4", q4.Text, true, true);
        document.Replace("Q5", q5.Text, true, true);
        document.Replace("Q6", q6.Text, true, true);
        document.Replace("Q7", q7.Text, true, true);
        document.Replace("Q8", q8.Text, true, true);
        document.Replace("Q9", q9.Text, true, true);
        document.Replace("Q10", q10.Text, true, true);
        document.Replace("Q11", q11.Text, true, true);

        document.Replace("W1", w1.Text, true, true);
        document.Replace("W2", w2.Text, true, true);
        document.Replace("W3", w3.Text, true, true);
        document.Replace("W4", w4.Text, true, true);
        document.Replace("W5", w5.Text, true, true);
        document.Replace("W6", w6.Text, true, true);
        document.Replace("W7", w7.Text, true, true);
        document.Replace("W8", w8.Text, true, true);
        document.Replace("W9", w9.Text, true, true);
        document.Replace("W10", w10.Text, true, true);
        document.Replace("W11", w11.Text, true, true);

        document.Replace("E1", e1.Text, true, true);
        document.Replace("E2", e2.Text, true, true);
        document.Replace("E3", e3.Text, true, true);
        document.Replace("E4", e4.Text, true, true);
        document.Replace("E5", e5.Text, true, true);
        document.Replace("E6", e6.Text, true, true);
        document.Replace("E7", e7.Text, true, true);
        document.Replace("E8", e8.Text, true, true);
        document.Replace("E9", e9.Text, true, true);
        document.Replace("E10", e10.Text, true, true);
        document.Replace("E11", e11.Text, true, true);

        document.Replace("F1", f1.Text, true, true);
        document.Replace("F2", f2.Text, true, true);
        document.Replace("F3", f3.Text, true, true);
        document.Replace("F4", f4.Text, true, true);
        document.Replace("F5", f5.Text, true, true);
        document.Replace("F6", f6.Text, true, true);
        document.Replace("F7", f7.Text, true, true);
        document.Replace("F8", f8.Text, true, true);
        document.Replace("F9", f9.Text, true, true);
        document.Replace("F10", f10.Text, true, true);
        document.Replace("F11", f11.Text, true, true);

        document.SaveToFile(@"h:\root\home\btslpc-001\www\site1\Sample.doc", FileFormat.Doc);

        //Launching the MS Word file.
        WordDocViewer("Sample.doc");
    }


When I click "Generate Card," it does nothing. It only refreshes the page and doesn't open any doc. However, it works on local host. I'm looking to have this working online. Any help would be appreciated. Thanks.

qbranwen
 
Posts: 2
Joined: Tue Jan 19, 2016 7:09 am

Tue Jan 19, 2016 7:27 am

Hi,

Thanks for your posting and using our Spire.Doc.
Please try saving method:
Code: Select all
document.SaveToFile("Sample.doc", FileFormat.Doc, Response, HttpContentType.Attachment);
.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3046
Joined: Wed Jun 27, 2012 8:50 am

Tue Jan 19, 2016 7:43 am

amy.zhao wrote:Hi,

Thanks for your posting and using our Spire.Doc.
Please try saving method:
Code: Select all
document.SaveToFile("Sample.doc", FileFormat.Doc, Response, HttpContentType.Attachment);
.

Best Regards,
Amy
E-iceblue support team


Thanks so much! Worked like a charm!

qbranwen
 
Posts: 2
Joined: Tue Jan 19, 2016 7:09 am

Tue Jan 19, 2016 8:00 am

Hi,

Thanks for your feedback.
I am glad that your issue has been resolved.
Welcome to write to us again for further problems.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3046
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc