Opening page as a Word Document

Discussion in 'ASP .Net' started by =?Utf-8?B?V2FubmFiZQ==?=, Feb 28, 2007.

  1. I am creating a webpage and opening it as a Word document. Problem is that as
    I create one of the questions, some of the text is not appearing in the
    document as it should. As I step through it, I can see the text in my string
    builder variable, but once the page opens as a Word document, that same text
    is not showing. It seems to only be text that I am displaying as a result of
    some checkboxes I am trying to write to the page. Can anyone help with this?
    I've included the two methods that are used on the page, Page_Load and a
    method it calls, LoadSurvey. Thank you.

    private void Page_Load(object sender, System.EventArgs e)
    {
    //build the content for the dynamic Word document
    //in HTML alongwith some Office specific style properties.
    m_documentBody.Append("<html " +
    "xmlns:eek:='urn:schemas-microsoft-com:eek:ffice:eek:ffice' " +
    "xmlns:w='urn:schemas-microsoft-com:eek:ffice:word'" +
    "xmlns='http://www.w3.org/TR/REC-html40'>" +
    "<head><title>Survey</title>");

    //The setting specifies document's view after it is downloaded as Print
    //instead of the default Web Layout
    m_documentBody.Append("<!--[if gte mso 9]>" +
    "<xml>" +
    "<w:WordDocument>" +
    "<w:View>Print</w:View>" +
    "<w:Zoom>100</w:Zoom>" +
    "<w:DoNotOptimizeForBrowser/>" +
    "</w:WordDocument>" +
    "</xml>" +
    "<![endif]-->");

    m_documentBody.Append("<style>" +
    "<!-- /* Style Definitions */" +
    "@page Section1" +
    " {size:8.5in 11.0in; " +
    " margin:1.0in 1.0in 1.0in 1.0in ; " +
    " mso-header-margin:.5in; " +
    " mso-footer-margin:.5in; mso-paper-source:0;}" +
    " div.Section1" +
    " {page:Section1;}" +
    "-->" +
    "</style></head>");

    m_documentBody.Append("<body lang=EN-US style='tab-interval:.5in'>" +
    "<div class=Section1>");

    LoadSurvey(Request.QueryString.Get("surveyID"));

    m_documentBody.Append("</div></body></html>");

    //Force this content to be downloaded as a Word document.
    Response.AppendHeader("Content-Type", "application/msword");
    Response.AppendHeader ("Content-disposition", "attachment;
    filename=survey.doc");
    Response.Write(m_documentBody);
    }




    private void LoadSurvey(string surveyID)
    {
    DataTable mainQuestions = Question.List(surveyID);

    for(int index = 0; index <= mainQuestions.Rows.Count - 1; index++)
    {
    Label label = new Label();
    label.Font.Size = 11;

    if (m_documentBody.Length > 610)
    m_documentBody.Append("<br><br>");

    if (mainQuestions.Rows[index]["QuestionIdentifier"].ToString() !=
    string.Empty)
    label.Text =
    mainQuestions.Rows[index]["QuestionIdentifier"].ToString().Trim() + " " +
    mainQuestions.Rows[index]["QuestionText"];
    else
    label.Text = mainQuestions.Rows[index]["QuestionText"].ToString().Trim();

    m_documentBody.Append(label.Text);

    //Get the building blocks that belong to the current question.
    Question questions = Question.GetBuildingBlocks(null,
    mainQuestions.Rows[index]["QuestionID"].ToString());

    foreach(Question.BuildingBlock buildingBlock in
    questions.QuestionBuildingBlocks)
    {
    bool addLabel = false;
    Label bbLabel = new Label();

    if (buildingBlock.Identifier.Trim() != string.Empty)
    {
    m_documentBody.Append("<br>");
    bbLabel.Text = buildingBlock.Identifier.Trim() + " ";
    addLabel = true;
    }

    if (buildingBlock.BuildingBlockText.Trim() != string.Empty)
    {
    m_documentBody.Append("<br>");
    bbLabel.Text += buildingBlock.BuildingBlockText.Trim();
    addLabel = true;
    }

    if (addLabel)
    {
    m_documentBody.Append("<br>");
    bbLabel.Text = bbLabel.Text.Trim();
    m_documentBody.Append(bbLabel.Text);
    }

    if (buildingBlock.BuildingBlockType.ToString() == "Text" ||
    buildingBlock.BuildingBlockType.ToString() == "LongAnswer")
    {
    m_documentBody.Append("<br>");
    m_documentBody.Append("<br>");
    m_documentBody.Append("<br>");
    m_documentBody.Append("<br>");
    m_documentBody.Append("<br>");
    m_documentBody.Append("<br>");
    }
    else
    {
    CheckBoxList checkBoxList = new CheckBoxList();
    checkBoxList.Style.Add("margin-left", "35");
    checkBoxList.RepeatColumns = 4;
    checkBoxList.RepeatDirection = RepeatDirection.Vertical;
    checkBoxList.RepeatLayout = RepeatLayout.Table;
    checkBoxList.DataValueField = "AnswerOptionID";
    checkBoxList.DataTextField = "AnswerOption";
    checkBoxList.DataSource = Answer.ListOptions(buildingBlock.AnswerGroupID);
    checkBoxList.DataBind();
    m_documentBody.Append("<br>");

    for (int bbIndex = 0; bbIndex <= checkBoxList.Items.Count - 1; bbIndex++)
    {
    m_documentBody.Append(" ___");
    m_documentBody.Append(checkBoxList.Items[bbIndex].Text);
    }
    }
    }
    }
    }
     
    =?Utf-8?B?V2FubmFiZQ==?=, Feb 28, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Opening a Word Document

    Charles A. Lackman, Apr 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,204
    =?Utf-8?B?VGFyYXM=?=
    Apr 28, 2005
  2. =?Utf-8?B?U2FpZA==?=
    Replies:
    0
    Views:
    509
    =?Utf-8?B?U2FpZA==?=
    Nov 18, 2003
  3. Stephen Witter

    opening a word doc in word not browser

    Stephen Witter, May 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    525
    Stephen Witter
    May 18, 2004
  4. =?Utf-8?B?dGVycnk=?=
    Replies:
    2
    Views:
    603
    Paul Glavich [MVP - ASP.NET]
    Jul 9, 2004
  5. mkorch17

    Opening Word document from an ASP page

    mkorch17, Apr 6, 2006, in forum: ASP General
    Replies:
    7
    Views:
    228
Loading...

Share This Page