Read and Wirite Data InTo Ms-Word Using Asp.Net

In this article i am going to describe how to read and write to ms word file or document in ASP.NET using FileStream, StreamReader and StreamWriter.

For this i've added a textbox for input text to be written in word document and a button to write, same for reading text from word file.

The word file created is saved in a folder named document in root of application. which can be changed to desired location.




html source of the page look like
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server" 
                         onclick="Button1_Click" 
                         Text="Click to write to word" />
<br />
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox>
<asp:Button ID="Button2" runat="server" 
                         onclick="Button2_Click" 
                         Text="Read Word Document" />
</div>
</form>

To use Filestream and stringBuilder we need to add these namespace
using System.IO;
using System.Text;

C# code behind
protected void Button1_Click(object sender, EventArgs e)
{
    //Create stringBuilder to write formatted 
    //Text to word file
 StringBuilder strBuilder = new StringBuilder();
 strBuilder.Append("<h1 title='Header' align='Center'>
     Writing To Word Using ASP.NET</h1> ".ToString());

 strBuilder.Append("<br>".ToString());
 strBuilder.Append("<table align='Center'>".ToString());
 strBuilder.Append("<tr>".ToString());

 strBuilder.Append("<td style='width:100px;color:green'>
                          <b>amiT</b></td>".ToString());

 strBuilder.Append("<td style='width:100px;color:red'>
                              India</td>".ToString());
 strBuilder.Append("</tr>".ToString());
 strBuilder.Append("</table>".ToString());

 string strPath = Request.PhysicalApplicationPath
                         + "\\document\\Test.doc";

 //string strTextToWrite = TextBox1.Text;
 FileStream fStream = File.Create(strPath);
 fStream.Close();
 StreamWriter sWriter = new StreamWriter(strPath);
 Writer.Write(strBuilder);
 sWriter.Close(); 

}
protected void Button2_Click(object sender, EventArgs e)
{
 string strPath = Request.PhysicalApplicationPath 
                  + "\\document\\Test.doc";
 FileStream fStream = new FileStream
            (strPath, FileMode.Open, FileAccess.Read);
 StreamReader sReader = new StreamReader(fStream);
 TextBox2.Text = sReader.ReadToEnd();
 sReader.Close();
Response.Write(TextBox2.Text);
}

VB.NET code
Protected Sub Button1_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim strBuilder As New StringBuilder()
strBuilder.Append("<h1 title='Header' align='Center'>
     Writing To Word Using ASP.NET</h1> ".ToString())
strBuilder.Append("<br>".ToString())
strBuilder.Append("<table align='Center'>".ToString())
strBuilder.Append("<tr>".ToString())

strBuilder.Append("<td style='width:100px;color:green'>
                           <b>amiT</b></td>".ToString())
strBuilder.Append("<td style='width:100px;color:red'>
                               India</td>".ToString())
strBuilder.Append("</tr>".ToString())
strBuilder.Append("</table>".ToString())
'string path = @"C:\Test.doc";

Dim strPath As String = 
Request.PhysicalApplicationPath & "\document\Test.doc"
    
'string strTextToWrite = TextBox1.Text;
Dim fStream As FileStream = File.Create(strPath)
fStream.Close()
Dim sWriter As New StreamWriter(strPath)
sWriter.Write(strBuilder)
      
sWriter.Close()
End Sub

Protected Sub Button2_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim strPath As String = 
Request.PhysicalApplicationPath & "\document\Test.doc"

Dim fStream As New FileStream
(strPath, FileMode.Open, FileAccess.Read)
Dim sReader As New StreamReader(fStream)
TextBox2.Text = sReader.ReadToEnd()
sReader.Close()
Response.Write(TextBox2.Text)
End Sub

Refresh Page After Back Button Click

I have to do something like this in a catalog where the browse page 
needs to be loaded from the DB on every load because when you hit a 
product page it calls out to a 3rd party to get updated info, and then 
save it if it should be updated. This is so when you hit the back button
 like you're saying the data is reloaded. What I've done is added this 
into the page and it seems to work fine in all browsers. 
 
 
 
public class ProductBrowser : Page
{
    protected override void OnInit(EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);

        base.OnInit(e);
    }
}

Cm to Pixel Converter


Cm to pixels



Calculate the pixel size of an image from the width and height in cm, and the resolution


Height   cm
Width   cm
Resolution   dpi

 
 
     
Height in pixels
 
Width in pixels  


Converter in Asp.Net

Using the Pixel (X) To Centimeter [cm] Converter

This online Unit Conversion page allows quick and accurate conversion between many units of measure, from one system to another. The Unit Conversion page provides a solution for anyone whose activities require working with quantities measured in different units.
You can use this online converter to convert between several hundred units (including metric, British and American) in 76 categories, or several thousand pairs including acceleration, area, electrical, energy, force, length, light, mass, mass flow, density, mass capacity, power, pressure, stress, temperature, time, torque, velocity, viscosity, volume and capacity, volume flow, and more.
  • Select the unit to convert from in the left box containing the list of units.
  • Select the unit to convert to in the right box containing the list of units.
  • Enter the value (for example "15") into the left From box.
  • The result will appear in the Result box and in the To box.
  • Alternatively, you can enter the value into the right To box and read the result of conversion in the From and Result boxes.


Click Heare TransaLate

Crystal Reports: The maximum report processing jobs limit configured by your system administrator has been reached.

Crystal Reports: The maximum report processing jobs limit configured by your system administrator has been reached.

This exception is produced by Crystal Reports when the maximum number of concurrent report processes have been initiated. I have had troubles with this exception in my asp.net application, but at first it seemed almost to occur at random. Recycling the app pool solved the problem when it occurred. After a while (when the application was used more intensively) this became a real problem, as it began to occur more and more often.

My application does not use a ReportViewer, but rather generates a pdf file and sends it to the client as a download, using the following code:
ReportDocument repDoc = new ReportDocument();

repDoc.Load(filename);
// update the crystal report database connection
repDoc.SetCrystalReportDatabase("ConnectionString");

// send the resulting document to the user (as PDF).
Response.ClearContent();
Response.ClearHeaders();

repDoc.ExportToHttpResponse(formatType, Response, true, "Report");

repDoc.Dispose();

At first, I fixed this problem quite easily, by altering one value in the registry:

HKEY_LOCAL_MACHINE\SOFTWARE\Business Objects\Suite 11.5\Report Application 
Server\InprocServer\PrintJobLimit



Go to the Start button > Run.
Type in "regedit"
This opens up the Registry Editor.
You will find the subtree category "HKEY_LOCAL_MACHINE" here.

It is set to 75 by default, I set it to 250. (Use regedit.exe to change this, but be careful, you can really mess up Windows if you make mistakes while altering the registry!)



But after some time (when the application was used even more intensively) the problem returned, so I looked into it again and started looking for a more definite solution.


I found that for some reason the Dispose(); in my code did not get called properly (I think the ExportToHttpResponse call throws an exception, although I did not check this).
When I changed the code to:
using(ReportDocument repDoc = new ReportDocument())
{
   repDoc.Load(filename);
   // update the crystal report database connection
   repDoc.SetCrystalReportDatabase("ConnectionString");

   // send the resulting document to the user (as PDF).
   Response.ClearContent();
   Response.ClearHeaders();

   repDoc.ExportToHttpResponse(formatType, Response, true, "Report");

}
The problem was fixed ... at least for now.

I have also found out that if you use a viewer, you will have to maintain a reference to the ReportDocument in a private variable on your page. In the Page_Unload() event, you can then close and dispose of this object, so that it is cleared from memory, like so:
class MyPage : Page
{
    private ReportDocument repDoc = null;

    Page_Load(object sender, EventArgs e)
    {
        this.repDoc = new ReportDocument();
        this.viewer.ReportSource = repDoc;
    }


    Page_Unload(object sender, EventArgs e)
    {
        if(this.repDoc != null)
        {
            this.repDoc.Close();
            this.repDoc.Dispose();
        }
    }
}