asp.net fill Word template labels and download the use of notes

Source: Internet
Author: User
Tags garbage collection

1, create a new Word template by looking for a Word document that needs to be populated with a program, and saving it if you want to enter it with a bookmark (insert--> bookmark--> enter Id-->ok).
2, modify the security add everyone readable on the word template to prevent the file from being opened.
3, add "References" to the project to find the "Microsoft Word 10.0 Object Library" or "Microsoft Word 11.0 Object Library" point determination.
4, new class called WordOp.cs meaning is the class that operates word.
The contents are as follows:

/////////////////////////////////////////////////////////////////////////////
Using System;
Using System.Web.Security;
Using Microsoft.Office.Interop.Word;
Using System.IO;
<summary>
Word's summary description
</summary>
public class Wordop
{
Public Wordop ()
{
//
TODO: Add constructor logic here
//
}
Private ApplicationClass WordApp;
Private Document WordDoc;
private static bool isopened=false;//to determine if Word templates are occupied
public void SaveAs (String Strfilename,bool isreplace)
{
if (Isreplace && file.exists (strFileName))
{
File.delete (strFileName);
}
Object missing = Type.Missing;
Object fileName = strFileName;
Worddoc.saveas (ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
Ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
Define a Word.Application Object
public void Activewordapp ()
{
WordApp = new ApplicationClass ();
}

public void Quit ()
{
Object missing = System.Reflection.Missing.Value;
WordApp.Application.Quit (ref missing, ref missing, ref missing);
isopened = false;
}

New Word files based on templates
public void Opentempelte (string strtemppath)
{
Object Missing = Type.Missing;
Object Missing = System.Reflection.Missing.Value;

Activewordapp ();
Wordapp.visible = false;

Object otemplate = (object) Strtemppath;
Try
{
while (isopened)
{
System.Threading.Thread.Sleep (500);
}
WordDoc = WORDAPP.DOCUMENTS.ADD (ref otemplate, ref Missing, ref Missing, ref Missing);
Isopened = true;
Worddoc.activate ();
}
catch (Exception Ex)
{
Quit ();
isopened = false;
throw new Exception (ex.message);
}


}
public void Filllable (String labelid,string Content)
{

Open Word Template
Opentempelte (Tempname); Fill in the contents of the Labelid label, that is, the subject item of the letter
Object BKMC = Labelid;
if (WordApp.ActiveDocument.Bookmarks.Exists (labelid) = = True)
{
WordApp.ActiveDocument.Bookmarks.get_Item (ref BKMC). Select ();

}
WordApp.Selection.TypeText (Content);
SaveAs (Saveasfilename);
Quit ();

}
}

/////////////////////////////////////////////////////////////////////////////

5, in the need to use the page background call can be, for example as follows:

String path = Server.MapPath ("Download");
String TemplatePath = path + "";
Wordop wop = new Wordop ();
Wop. Opentempelte (TemplatePath);
Wop. Filllable ("id", "1");
Wop. Filllable ("Usr_name", "Test");
Wop. SaveAs (path + ");
Wop. Quit ();
Response.Redirect (@ "/download/savetest.doc");//Make a jump for downloading.

Description: This method requires the addition of. NET frame1.1 support when installing Office 2003, select it in the advanced level, or you will report to find a Micosoft.Office.Interop.Word error

Precautions :

ASPNET uses ms&nbsp;word&nbsp;excel template problem

 

Exception Details: system.unauthorizedaccessexception: F:/xxxx.xls on paths

Be denied access

Solution One

Add the Network Service account in the directory where read and write operations are required.

Because in IIS 6.0, the default application pool is marked with the network Service,

So in the process is using the Network service this account to run the W3wp.exe process,

And when we run VS 2003, we need to read and write to a directory.

Looked at the folder, found no network Service, add the account, and select the full control, problem solution

Summary

Access Word template permissions without setting a user name and password

Application The following error occurs when the program runs to this sentence: Retrieves a COM class factory with the CLSID

The component of {000209ff-0000-0000-c000-000000000046} failed because of the following error:

80070005.

3. Click the Security tab, select Custom in the start and Activate permissions and access permissions groups, and then

Custom-> edit-> add asp.net account and iuser_ computer name.

Workaround two: If the above method does not solve the problem, it should be a permission issue, please try the following

Method: Use identity simulation in Web.config, add in section

The user name and password are not used

Workaround One:

1: Install the Office Word software on the server.

2: Enter Dcomcnfg.exe in Start-> Run to Start Component Services

3: Double-click "Component Services"-> "Computer"-> "My Computer"-

> "DCOM Configuration"

4: In the DCOM configuration, locate the Microsoft

Word application, right-click on it, and then click Properties to Eject Microsoft

Word Application Properties dialog box

5: Click on the "Logo" tab, select "Interactive User"

6: Click the "Security" tab, click "Customize" on "Start and Activate Permissions", then click the corresponding "Edit" button and add in the "Security" dialog box that pops up

A "network

SERVICE User (Note that you want to select this computer name) and give it local startup and local activation permissions.

7: Remain the "Security" tab, click "Customize" on "Access" and click "Edit" and add a "network" in the "Security" dialog box that pops up.

SERVICE user, and then give the local access permission.

In this way, we have configured the appropriate DCOM permissions for Word.

Note: This is configured on the WIN2003, on 2000, may be configured to configure the ASPNET user

Because Word is open on the server, you should write a method to save the exported data on the server and then pass it on to the client, and then delete any word that was previously generated on the server every time you call this feature

Retrieves the CLSID {000209ff-0000-0000-c000-000000000046} in a COM class factory

Component fails because the following error occurred: 80070005

In csdn always have netizens ask this question, oneself also met, because some write for reference:

Symptoms:

Owordapplic = New Word.Application

The following error occurred while the program was running into this sentence:

Retrieves the CLSID {000209ff-0000-0000-c000-000000000046} in a COM class factory

Component fails because the following error occurred: 80070005.

Owordapplic = New Word.Application

The following error occurred while the program was running into this sentence:

Retrieves the CLSID {000209ff-0000-0000-c000-000000000046} in a COM class factory

Component fails because the following error occurred: 80070005.

Workaround One:

Control Panel-"Management tools-" Component Services-"computer-" My Computer-"DCOM configuration-" Find a Microsoft Word document

After

Click Properties to open the Properties dialog box for this application.

2. Click the Identification tab, and then select the Interactive user.

3. Click the Security tab, select Custom in the start and Activate permissions and access permissions groups, and then

Custom-> edit-> add asp.net account and iuser_ computer name

* These accounts only exist if IIS is installed on the computer.

13. Ensure that each user is allowed access, and then click OK.

14. Click OK to close DCOMCNFG.

Workaround Two:

If the above method does not resolve the problem, it should be a permission issue, please try the following method:

Using identity simulations in web.config,

section, add

1.webconfig file

2. Reference namespace: using using Microsoft.Office;

Before that, you need to add a reference, right-click the item, add a reference, browse C:Program FilesMicrosoft

OfficeOFFICE11 Find Excel.exe OK

3. Calling methods

String sql = "SELECT top uid as user number, username as user name from

Dnt_users ";

ds = Sqlhelp.executedataset (Sqlhelp.sqlcon, CommandType.Text, SQL,

NULL);

exportToExcel (DS,

Server.MapPath ("excel/" +datetime.now.tostring ("YYYYMMDDHHSSM"));

4. Methods

#region

from DataSet to Excel

Export the specified Excel file

public void exportToExcel (DataSet ds, String

Strexcelfilename)

{

if (ds. Tables.count = 0 | | Strexcelfilename = = "") return;

Doexport (ds, Strexcelfilename);

}

Performing an export

private void Doexport (DataSet ds, String strexcelfilename)

{

Excel.Application excel1 = new Excel.Application ();

int rowIndex = 1;

int colindex = 0;

Excel1. APPLICATION.WORKBOOKS.ADD (TRUE);

System.Data.DataTable table = ds. Tables[0];

foreach (DataColumn col in table). Columns)

{

colindex++;

Excel1. Cells[1, Colindex] = Col. ColumnName;

}

foreach (DataRow row in table). Rows)

{

rowindex++;

Colindex = 0;

foreach (DataColumn col in table). Columns)

{

colindex++;

Excel1. Cells[rowindex, Colindex] =

Row[col. ColumnName]. ToString ();

}

}

Excel1. Visible = false;

Excel1. ActiveWorkbook.SaveAs (Strexcelfilename + ". XLS ",

Excel.XlFileFormat.xlExcel9795, NULL, NULL, FALSE, FALSE,

Excel.XlSaveAsAccessMode.xlNoChange, NULL, NULL, NULL, NULL,

NULL);

Excel1. Quit ();

Excel1 = null;

Gc. Collect ()//garbage collection

}

#endregion

5. Server Configuration

Retrieving the COM class factory for component with CLSID

{00024500-0000-0000-c000-000000000046} failed due to the following

error:80070005.

Retrieves the CLSID in the COM class factory is {00024500-0000-0000-c000-000000000046}

Component fails because the following error occurred: 80070005

1: Install Office Excel software on the server.

2: In the "Start"-> "Run" Enter Dcomcnfg.exe start "Component Services"

3: Double-click "Component Services"-> "Computer"-> "My Computer"-> "DCOM Configuration"

4: In the DCOM configuration, find Microsoft Excel

Application, right-click on it, and click Properties to eject the Microsoft Excel Application Properties dialog box

5: Click on the "Logo" tab, select "Interactive User"

6: Click the "Security" tab, click "Customize" on "Start and activate permission", then click the corresponding "Edit" button, and add a "network" in the "Security" dialog box that pops up.

SERVICE User (Note that you want to select this computer name) and give it local startup and local activation permissions.

7: Remain the "Security" tab, click "Customize" on "Access" and click "Edit" and add a "network" in the "Security" dialog box that pops up.

SERVICE user, and then give the local access permission.

In this way, we have configured the appropriate Excel DCOM permissions.

Note: I was configured on the WIN2003, on 2000, but configuring the ASPNET user

Retrieving the COM class factory for component with CLSID

{00020819-0000-0000-c000-000000000046} failed due to the following

error:80040154

1. Run DCOMCNFG to open Component Services,

2. Expand Component Services-> "Computer"-> "My Computer"-> "DCOM Configuration" in turn

3. Find a Microsoft Excel application or Microsoft Word application

4. Right-click to open the Properties dialog box and tap the Identification tab.

5. Click the "following users", the administrator of the user password correctly fill in ...

6. Click on the "Security" tab,

7. In turn, "Start and Activate Permissions", "Access rights", "Configure Permissions", are selected as a custom,

8. Then click on their edits, add everyone (or user), and join all the permissions ...

Retrieves the CLSID {00024500-0000-0000-c000-000000000046} in a COM class factory

Component fails because the following error occurred: 8000401a.

Note that the wrong category, you need to manually login to the server is to connect the monitor, keyboard, generally placed in the room may not be able to solve the

You can use the following as a workaround, and remember not to log off after logging in.

In Microsoft Windows Server 2003, log on to the CONSOLE0/console

Switch to resolve the problem. To do this, follow these steps:

1. Click Start, click Run, type Mstsc/console, and then click OK.

2. Log on to the computer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.