Opening page as a Word Document

G

Guest

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top