Monday, January 31, 2011

Create Word 2007 document using OpenXML SDK with font size, font color and bold formatting

 

Step 1
Install OpenXML SDK 2.0. You can download from here:

Step 2
Add these assemblies:

  • OpenXML SDK (browse to the folder where you installed OpenXML SDK)
  • Windows Base

<assemblies><add assembly="DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies>

Step 3
Import these :
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using System.Xml;
using System.IO;
using System.Text;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Collections;

Step 4
Create a class that looks like this:

public class MyParagraph
{
private string paragraphType;
private string text;

public MyParagraph()
{
}

public MyParagraph(string pt, string t)
{
this.paragraphType = pt;
this.text = t;
}

public string ParagraphType
{
get { return paragraphType; }
set { paragraphType = value; }
}

public string Text
{
get { return text; }
set { text = value; }
}
}
Step 5
Create this method
private void GenerateDocument(string filename, ArrayList myparagraphs)
{

WordprocessingDocument doc =
WordprocessingDocument.Create(filename,
WordprocessingDocumentType.Document);
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();

/* Create the contents */
for (int i = 0; i < myparagraphs.Count; i++)
{
DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run();
MyParagraph p = (MyParagraph)myparagraphs[i];
string txt = p.Text;
if (p.ParagraphType.Equals("Header1"))
{
DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties = new DocumentFormat.OpenXml.Wordprocessing.RunProperties();
runProperties.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Bold());
DocumentFormat.OpenXml.Wordprocessing.FontSize fs = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
fs.Val = "50";
DocumentFormat.OpenXml.Wordprocessing.Color c = new DocumentFormat.OpenXml.Wordprocessing.Color();
c.Val = "red";
runProperties.AppendChild(fs);
runProperties.AppendChild(c);
run.AppendChild(runProperties);
run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text(txt));
}
else
{
run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text(txt));
}
Paragraph para = new Paragraph(run);
body.AppendChild(para);
}

mainPart.Document.Append(body);

/* Save the results and close */
mainPart.Document.Save();
doc.Close();
}
 
Step 6
Call the function defined in Step 5 from an ASP.NET document

private void Generate(){
private void Generate()
{
MyParagraph p1 = new MyParagraph("Header1", "Line 1");
MyParagraph p2 = new MyParagraph("Header2", "Line 2");
MyParagraph p3 = new MyParagraph("Header3", "Line 3");
MyParagraph p4 = new MyParagraph("Header4", "Line 4");
ArrayList list = new ArrayList();
list.Add(p1); list.Add(p2); list.Add(p3); list.Add(p4);
GenerateDocument(Server.MapPath("Output\\Document.docx"),list);
}

Generating Microsoft Office Documents with the Open XML SDK

Generating Microsoft Office Documents with the Open XML SDK

Word 2010 Technical Articles

Word 2010 Technical Articles

Creating Open XML WordprocessingML Tables with Vertically Merged Cells

Creating Open XML WordprocessingML Tables with Vertically Merged Cells

Manipulating Word 2007 Files with the Open XML Format API (Part 3 of 3)

Manipulating Word 2007 Files with the Open XML Format API (Part 3 of 3)

Download details: Open XML SDK 2.0 for Microsoft Office

Download details: Open XML SDK 2.0 for Microsoft Office

Generating Microsoft Office Documents with the Open XML SDK

Generating Microsoft Office Documents with the Open XML SDK

Download details: Open XML SDK 2.0 for Microsoft Office

Download details: Open XML SDK 2.0 for Microsoft Office

Generating Microsoft Office Documents with the Open XML SDK

Generating Microsoft Office Documents with the Open XML SDK

Manipulating Word 2007 Files with the Open XML Format API (Part 2 of 3)

Manipulating Word 2007 Files with the Open XML Format API (Part 2 of 3)

Manipulating Word 2007 Files with the Open XML Format API (Part 1 of 3)

Manipulating Word 2007 Files with the Open XML Format API (Part 1 of 3)

Creation of a Word 2007 document using the Open XML Format SDK - CodeProject

Creation of a Word 2007 document using the Open XML Format SDK - CodeProject

SaguiItay

SaguiItay

SaguiItay: WordprocessingML: Part 1

SaguiItay: WordprocessingML: Part 1

HTML to WordML - CodeProject

HTML to WordML - CodeProject

Rendering WordML from ASP.NET - ASP.NET answers

Rendering WordML from ASP.NET - ASP.NET answers

Data-driven document generation with Word 2007 and the Office XML File Formats: Part 1 - Erika Ehrli - Site Home - MSDN Blogs

Data-driven document generation with Word 2007 and the Office XML File Formats: Part 1 - Erika Ehrli - Site Home - MSDN Blogs

How to use the Office XML file format and the packaging components from the .NET Framework 3.0 to create a simple Excel 2007 workbook or a simple Word 2007 document

How to use the Office XML file format and the packaging components from the .NET Framework 3.0 to create a simple Excel 2007 workbook or a simple Word 2007 document

Considerations for server-side Automation of Office

Considerations for server-side Automation of Office

Saturday, January 29, 2011

Silicon Valley Elite Flock To Y Combinator Demo Day

Silicon Valley Elite Flock To Y Combinator Demo Day

Y Combinator: What We Do

Y Combinator: What We Do: "The most important thing we do is work with startups on their ideas. We're hackers ourselves, and we've spent a lot of time figuring out how to make things people want. So we can usually see fairly quickly the direction in which a small idea should be expanded, or the point at which to begin attacking a large but vague one."

Y Combinator - Wikipedia, the free encyclopedia

Y Combinator - Wikipedia, the free encyclopedia: "rovides seed money, advice, and connections at two 3-month programs per year. In exchange, they take an average of about 6% of the company's equity.[1]"

Y Combinator | CrunchBase Profile

Y Combinator | CrunchBase Profile: "t offers financing as well as business consulting along with other opportunities to 2-4 person companies looking to take an idea to a product. Y Combinator looks for companies with “good” ideas over companies with experience and a business model."

Friday, January 28, 2011

Friday, January 7, 2011

I came across this error when I was coding a file that updated a table using SqlDataSource with a Stored Procedure.

""Procedure or function "  .... "has too many arguments specified"

Googled for the keywords above and I found this suggestion to my problem from phynix at forums.asp.net.

Adding the code solved my problem totally! ^.^

Thank you phynix!


Add an event handle for your SqlDataSource's Updating event:
for example:
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
 protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
 {
  DbParameterCollection CmdParams = e.Command.Parameters;
  ParameterCollection UpdParams = ((SqlDataSourceView)sender).UpdateParameters;
  Hashtable ht = new Hashtable();
  foreach (Parameter UpdParam in UpdParams)
      ht.Add(UpdParam.Name, true);
  for (int i = 0; i < CmdParams.Count; i++)
  {
      if (!ht.Contains(CmdParams[i].ParameterName.Substring(1)))
          CmdParams.Remove(CmdParams[i--]);
  }
 }

Wednesday, January 5, 2011

Steps to generate an Excel file from a GridView

 

Assuming you have a databound GridView called GridView1 whose contents you want to export to an Excel file named “ExcelFile.xls” upon clicking a button with ID “ButtonExport”, there are two basic steps what you need to do.

Step 1: Add the code to the event handler for ButtonExport click event as follows:

protected void ButtonExport_Click(object sender, EventArgs e)
{
   Response.Clear();
   Response.Buffer = true;
   Response.AddHeader("content-disposition",
                      "attachment;filename=ExcelFile.xls");

   Response.Charset = "";
   Response.ContentType = "application/vnd.ms-excel";

   StringWriter sw = new StringWriter();
   HtmlTextWriter hw = new HtmlTextWriter(sw);

   GridView1.AllowPaging = false;
   GridView1.DataBind();
   GridView1.RenderControl(hw);

   Response.Output.Write(sw.ToString());
   Response.Flush();
   Response.End();
} //end ButtonExport_Click

Step 2: Add the VerifyRenderingInServerForm method

You will need to add this method in order for the code in Step 1 to work.

public override void VerifyRenderingInServerForm(Control control)
{
       return;
}

Formatting DateTime databound fields in ASP.NET in asp:Label

Here's how to format it in code-behind code:
/* Displays the date and time as "05 Jan 2011 16:25"in a Label control inside a FormView control */
DateTime datetime = DateTime.Now; Label lblDateOfEntry = (Label)FormView1.FindControl("LabelDateOfEntry") lblDateOfEntry.Text = String.Format("{0:dd MMM yyyy HH:mm}", datetime); 

And here's how to format it in Markup code

<asp:Label ID="LabelDateOfEntry" runat="server" Text='<%# Bind("DateOfEntry","{0:dd MMM yyyy HH:mm}") %>' >