By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Opening page as a Word Document

P: n/a
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:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office: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);
}
}
}
}
}

Feb 28 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.