asp.net 用后台代码实现打印服务器的word文档

yangpeiyu 2008-11-18 10:52:10
功能说明:

用户在网页上直接点击网页上的一个打印按钮,服务器就直接打印相对应的word文档出来。。


兄弟姐妹们,有搞过的快帮忙一下。。先谢谢!
...全文
650 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiwufengsha1234 2012-07-12
  • 打赏
  • 举报
回复
可以调用VBA,有这方面代码。网上找一下,很方便的,就是不知道现在对楼主有没有用了~
yangpeiyu 2008-11-19
  • 打赏
  • 举报
回复
...个贴停在这里,没有人来解决掉了。。
shenyong20080708 2008-11-19
  • 打赏
  • 举报
回复
至少对我有用谢了
shenyong20080708 2008-11-19
  • 打赏
  • 举报
回复
至少对我有用谢了
yangpeiyu 2008-11-19
  • 打赏
  • 举报
回复
还是自己研究算了.

如有路过者...留下意见!!!
wangbin1986 2008-11-19
  • 打赏
  • 举报
回复
帮忙顶下
yangpeiyu 2008-11-18
  • 打赏
  • 举报
回复
4楼。读出文本这个不符合我要求。不过不是要谢谢。。
我主要是客户端打印务器的word文档。。还有没有高人指点。
MOTA 2008-11-18
  • 打赏
  • 举报
回复
///public static void Main()
///{
/// OfficeFileReader.OfficeFileReader objOFR = new OfficeFileReader.OfficeFileReader()
/// string output="";
/// objOFR.GetText("C:\\MyWordFile.Doc", ref output);
/// Console.WriteLine(output);
///}
MOTA 2008-11-18
  • 打赏
  • 举报
回复

///==============================================================

/// Office File Reader

///==============================================================

using System;

using System.Text;

using System.Runtime.InteropServices;



namespace OfficeFileReader
{
#region Stuff you Dont even need to look at
[Flags]

public enum IFILTER_INIT
{

NONE = 0,

CANON_PARAGRAPHS = 1,

HARD_LINE_BREAKS = 2,

CANON_HYPHENS = 4,

CANON_SPACES = 8,

APPLY_INDEX_ATTRIBUTES = 16,

APPLY_CRAWL_ATTRIBUTES = 256,

APPLY_OTHER_ATTRIBUTES = 32,

INDEXING_ONLY = 64,

SEARCH_LINKS = 128,

FILTER_OWNED_VALUE_OK = 512

}



[Flags]

public enum IFILTER_FLAGS
{

OLE_PROPERTIES = 1

}



public enum CHUNK_BREAKTYPE
{

CHUNK_NO_BREAK = 0,

CHUNK_EOW = 1,

CHUNK_EOS = 2,

CHUNK_EOP = 3,

CHUNK_EOC = 4

}



[Flags]

public enum CHUNKSTATE
{

CHUNK_TEXT = 0x1,

CHUNK_VALUE = 0x2,

CHUNK_FILTER_OWNED_VALUE = 0x4

}



public enum PSKIND
{

LPWSTR = 0,

PROPID = 1

}



[StructLayout(LayoutKind.Sequential)]

public struct PROPSPEC
{

public uint ulKind;

public uint propid;

public IntPtr lpwstr;

}



[StructLayout(LayoutKind.Sequential)]

public struct FULLPROPSPEC
{

public Guid guidPropSet;

public PROPSPEC psProperty;

}



[StructLayout(LayoutKind.Sequential)]

public struct STAT_CHUNK
{

public uint idChunk;

[MarshalAs(UnmanagedType.U4)]
public CHUNK_BREAKTYPE breakType;

[MarshalAs(UnmanagedType.U4)]
public CHUNKSTATE flags;

public uint locale;

[MarshalAs(UnmanagedType.Struct)]
public FULLPROPSPEC attribute;

public uint idChunkSource;

public uint cwcStartSource;

public uint cwcLenSource;

}



[StructLayout(LayoutKind.Sequential)]

public struct FILTERREGION
{

public uint idChunk;

public uint cwcStart;

public uint cwcExtent;

}


#endregion

[ComImport]

[Guid("89BCB740-6119-101A-BCB7-00DD010655AF")]

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

public interface IFilter
{

void Init([MarshalAs(UnmanagedType.U4)] IFILTER_INIT grfFlags,

uint cAttributes,

[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] FULLPROPSPEC[] aAttributes,

ref uint pdwFlags);



void GetChunk([MarshalAs(UnmanagedType.Struct)] out STAT_CHUNK pStat);



[PreserveSig]
int GetText(ref uint pcwcBuffer, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder buffer);



void GetValue(ref UIntPtr ppPropValue);



void BindRegion([MarshalAs(UnmanagedType.Struct)]FILTERREGION origPos, ref Guid riid, ref UIntPtr ppunk);

}



[ComImport]

[Guid("f07f3920-7b8c-11cf-9be8-00aa004b9986")]

public class CFilter
{

}





public class Constants
{

public const uint PID_STG_DIRECTORY = 0x00000002;



public const uint PID_STG_CLASSID = 0x00000003;

public const uint PID_STG_STORAGETYPE = 0x00000004;



public const uint PID_STG_VOLUME_ID = 0x00000005;

public const uint PID_STG_PARENT_WORKID = 0x00000006;

public const uint PID_STG_SECONDARYSTORE = 0x00000007;



public const uint PID_STG_FILEINDEX = 0x00000008;

public const uint PID_STG_LASTCHANGEUSN = 0x00000009;

public const uint PID_STG_NAME = 0x0000000a;

public const uint PID_STG_PATH = 0x0000000b;



public const uint PID_STG_SIZE = 0x0000000c;

public const uint PID_STG_ATTRIBUTES = 0x0000000d;

public const uint PID_STG_WRITETIME = 0x0000000e;

public const uint PID_STG_CREATETIME = 0x0000000f;

public const uint PID_STG_ACCESSTIME = 0x00000010;

public const uint PID_STG_CHANGETIME = 0x00000011;



public const uint PID_STG_CONTENTS = 0x00000013;

public const uint PID_STG_SHORTNAME = 0x00000014;



public const int FILTER_E_END_OF_CHUNKS = (unchecked((int)0x80041700));

public const int FILTER_E_NO_MORE_TEXT = (unchecked((int)0x80041701));

public const int FILTER_E_NO_MORE_VALUES = (unchecked((int)0x80041702));



public const int FILTER_E_NO_TEXT = (unchecked((int)0x80041705));

public const int FILTER_E_NO_VALUES = (unchecked((int)0x80041706));



public const int FILTER_S_LAST_TEXT = (unchecked((int)0x00041709));


}
public class OfficeFileReader
{
public void GetText(String path,ref string text)
// path is the path of the .doc, .xls or .ppt file
// text is the variable in which all the extracted text will be stored
{
String result = "";
int count = 0;
try
{
IFilter ifilt = (IFilter)(new CFilter());
//System.Runtime.InteropServices.UCOMIPersistFile ipf = (System.Runtime.InteropServices.UCOMIPersistFile)(ifilt);
System.Runtime.InteropServices.ComTypes.IPersistFile ipf= (System.Runtime.InteropServices.ComTypes.IPersistFile)(ifilt);
ipf.Load(@path, 0);
uint i = 0;
STAT_CHUNK ps = new STAT_CHUNK();
ifilt.Init(IFILTER_INIT.NONE, 0, null, ref i);
int hr = 0;

while (hr == 0)
{

ifilt.GetChunk(out ps);
if (ps.flags == CHUNKSTATE.CHUNK_TEXT)
{
uint pcwcBuffer = 1000;
int hr2 = 0;
while (hr2 == Constants.FILTER_S_LAST_TEXT || hr2 == 0)
{
try
{
pcwcBuffer = 1000;
System.Text.StringBuilder sbBuffer = new StringBuilder((int)pcwcBuffer);
hr2 = ifilt.GetText(ref pcwcBuffer, sbBuffer);
// Console.WriteLine(pcwcBuffer.ToString());
if (hr2 >= 0) result += sbBuffer.ToString(0, (int)pcwcBuffer);
//textBox1.Text +="\n";
// result += "#########################################";
count++;
}
catch (System.Runtime.InteropServices.COMException myE)
{
Console.WriteLine(myE.Data + "\n" + myE.Message + "\n");

}
}
}

}

}
catch (System.Runtime.InteropServices.COMException myE)
{
Console.WriteLine(myE.Data + "\n" + myE.Message + "\n");

}

text = result;
//return count;
return;

}
}

}

提取DOC文件内容
奔跑的蜗牛007 2008-11-18
  • 打赏
  • 举报
回复
在网上找找有没有第三方的打印控件?
cpio 2008-11-18
  • 打赏
  • 举报
回复
服务器安装Word

以Com组件方式调用Word

具体实现可以找找网上的调用Excel或者Word的代码

要是自己试的话,先在引用里面添加Com引用,把Word添加进去,然后再实例化Word,打开文件,打印,退出。
yangpeiyu 2008-11-18
  • 打赏
  • 举报
回复
...汗。没人做过?
yangpeiyu 2008-11-18
  • 打赏
  • 举报
回复
8楼。你这个是把WORD输出到网页上。。

我的目的是好像直接打开服务器里的一个word文档就打印。。
wangbin1986 2008-11-18
  • 打赏
  • 举报
回复
//1.定义文档类型、字符编码
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.doc 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
Response.AppendHeader("Content-Disposition", "online;filename=Paper.doc");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || //application/ms-html || 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-word";
this.EnableViewState = false;
//2.定义一个输入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//3.将目标数据绑定到输入流输出
this.panel.RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
//toFiles(1);
yangpeiyu 2008-11-18
  • 打赏
  • 举报
回复
....没有高手了!。。。还没解决呢。。。顶起来。。解决再来分。

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧