GVKun编程网logo

jsp实现针对excel及word文档的打印方法(用jsp方式打印出6×6表格)

3

如果您对jsp实现针对excel及word文档的打印方法和用jsp方式打印出6×6表格感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解jsp实现针对excel及word文档的打印方法的各种细节,

如果您对jsp实现针对excel及word文档的打印方法用jsp方式打印出6×6表格感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解jsp实现针对excel及word文档的打印方法的各种细节,并对用jsp方式打印出6×6表格进行深入的分析,此外还有关于asp.net(c#)下读取word文档的方法小结、C# 复制一个Word文档的部分或全部内容到另一个Word文档、C#实现合并多个word文档的方法、C#实现简单合并word文档的方法的实用技巧。

本文目录一览:

jsp实现针对excel及word文档的打印方法(用jsp方式打印出6×6表格)

jsp实现针对excel及word文档的打印方法(用jsp方式打印出6×6表格)

本文实例讲述了jsp实现针对excel及word文档的打印方法。分享给大家供大家参考,具体如下:

因为ms word和excel的文档都支持html文本格式,因此可以先用word或excel做好模版,另存为Web页,然后将该html改成jsp,将数据部分动态填入即可,不用很辛苦的调整格式

word页面只要在jsp头设置如下指令:

复制代码 代码如下:
<%@page contentType="application/msword;charset=GBK" %>

excel如下:
复制代码 代码如下:
<%@page contentType="application/vnd.ms-excel;charset=GBK" %>

使用这种方式客户端必须安装有office软件,用户访问时将在ie中直接用word或excel打开该页面。

此方法优势是模板设计、调整方便,无需在服务器端使用复杂的POI或jxl技术,也无需在客户端使用ActiveX控件技术,更安全、方便,轻松实现较好的打印效果。

microsoft关于服务器端动态创建office文档的资料(asp示例):

http://support.microsoft.com/default.aspx?scid=KB;en-us;301044&

简单示例:

使用word建立一文档,画表格如下:

----------------------------
| 用户名 | 真实姓名 | 性别 |
----------------------------
| guest  | 路人甲   | 男   |
----------------------------

保存为Web页test.htm, 将test.htm改名为test.jsp,修改其中guest、路人甲、男为从数据库动态查询,如下:

<%@ page contentType="application/msword;charset=GBK" %>
<%@ page import="java.sql.*" %>
<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>
<meta http-equiv=Content-Type content="text/html; charset=GB2312">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 9">
<meta name=Originator content="Microsoft Word 9">
<title>用户信息</title>
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
 <o:Author>evan zhao</o:Author>
 <o:LastAuthor>evan zhao</o:LastAuthor>
 <o:Revision>1</o:Revision>
 <o:TotalTime>1</o:TotalTime>
 <o:Created>2003-08-20T16:26:00Z</o:Created>
 <o:LastSaved>2003-08-20T16:27:00Z</o:LastSaved>
 <o:Pages>1</o:Pages>
 <o:Company>taiping</o:Company>
 <o:Lines>1</o:Lines>
 <o:Paragraphs>1</o:Paragraphs>
 <o:Version>9.2812</o:Version>
 </o:DocumentProperties>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <w:WordDocument>
 <w:PunctuationKerning>
 <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
 <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
 <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
 <w:Compatibility>
  <w:SpaceForUL>
  <w:BalanceSingleByteDoubleByteWidth>
  <w:DoNotLeaveBackslashAlone>
  <w:ULTrailSpace>
  <w:DoNotExpandShiftReturn>
  <w:AdjustLineHeightInTable>
  <w:UseFELayout>
 </w:Compatibility>
 </w:WordDocument>
</xml><![endif]-->
<style>
<!--
 /* Font Definitions */
@font-face
  {font-family:宋体;
  panose-1:2 1 6 0 3 1 1 1 1 1;
  mso-font-alt:SimSun;
  mso-font-charset:134;
  mso-generic-font-family:auto;
  mso-font-pitch:variable;
  mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
  {font-family:"@宋体";
  panose-1:2 1 6 0 3 1 1 1 1 1;
  mso-font-charset:134;
  mso-generic-font-family:auto;
  mso-font-pitch:variable;
  mso-font-signature:3 135135232 16 0 262145 0;}
 /* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
  {mso-style-parent:"";
  margin:0cm;
  margin-bottom:.0001pt;
  text-align:justify;
  text-justify:inter-ideograph;
  mso-pagination:none;
  font-size:10.5pt;
  mso-bidi-font-size:12.0pt;
  font-family:"Times New Roman";
  mso-fareast-font-family:宋体;
  mso-font-kerning:1.0pt;}
 /* Page Definitions */
@page
  {mso-page-border-surround-header:no;
  mso-page-border-surround-footer:no;}
@page Section1
  {size:595.3pt 841.9pt;
  margin:72.0pt 90.0pt 72.0pt 90.0pt;
  mso-header-margin:42.55pt;
  mso-footer-margin:49.6pt;
  mso-paper-source:0;
  layout-grid:15.6pt;}
div.Section1
  {page:Section1;}
-->
</style>
</head>
<body lang=ZH-CNtab-interval:21.0pt;text-justify-trim:punctuation''>
<div class=Section1layout-grid:15.6pt''>
<table border=1 cellspacing=0 cellpadding=0border-collapse:collapse; border:none;mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt''>
 <tr>
 <td width=189 valign=topwidth:142.0pt;border:solid windowtext .5pt; padding:0cm 5.4pt 0cm 5.4pt''>
 <p class=MsoNormal><spanfont-family:宋体;mso-ascii-font-family:"Times New Roman"; mso-hansi-font-family:"Times New Roman"''>用户名</span></p>
 </td>
 <td width=189 valign=topwidth:142.05pt;border:solid windowtext .5pt;border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt''>
 <p class=MsoNormal><spanfont-family:宋体;mso-ascii-font-family:"Times New Roman"; mso-hansi-font-family:"Times New Roman"''>真实姓名</span></p>
 </td>
 <td width=189 valign=topwidth:142.05pt;border:solid windowtext .5pt; border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt''>
 <p class=MsoNormal><spanfont-family:宋体;mso-ascii-font-family:"Times New Roman"; mso-hansi-font-family:"Times New Roman"''>性别</span></p>
 </td>
 </tr>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:mydb";
//连接mydb数据库
Connection con=DriverManager.getConnection (url, "", "");
try{
 Statement stmt=con.createStatement();
 //查询employee表
 ResultSet rs=stmt.executeQuery("select user_name, real_name, gender from employee ");
 while(rs.next()){
%>
 <tr>
 <td width=189 valign=topwidth:142.0pt;border:solid windowtext .5pt;border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt''>
 <p class=MsoNormal><span lang=EN-US><%=rs.getString("user_name")%></span></p>
 </td>
 <td width=189 valign=topwidth:142.05pt;border-top:none;border-left:none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt''>
 <p class=MsoNormal><spanfont-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"''><%=rs.getString("real_name")%></span></p>
 </td>
 <td width=189 valign=topwidth:142.05pt;border-top:none;border-left:none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt''>
 <p class=MsoNormal><spanfont-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"''><%=rs.getString("gender")%></span></p>
 </td>
 </tr>
<%
 } // end while
 rs.close();
 stmt.close();
} finally {
 con.close();
}
%>
</table>
<p class=MsoNormal><span lang=EN-US><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
</div>
</body>
</html>

希望本文所述对大家JSP程序设计有所帮助。

您可能感兴趣的文章:
  • jsp页面中显示word/excel格式的文档的方法
  • JSP上传excel及excel插入至数据库的方法
  • JSP导出Excel文件的方法
  • JSP实现从数据库导出数据到Excel下载的方法
  • jsp导出excel并支持分sheet导出的方法
  • jsp利用POI生成Excel并在页面中导出的示例

asp.net(c#)下读取word文档的方法小结

asp.net(c#)下读取word文档的方法小结

第一种方法:
复制代码 代码如下:

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "Application/msword";
string s=Server.MapPath("C#语言参考.doc");
Response.WriteFile("C#语言参考.doc");
Response.Write(s);
Response.Flush();
Response.Close();

第二种方法:
复制代码 代码如下:

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "Application/msword";
string strFilePath="";
strFilePath =Server.MapPath("C#语言参考.doc");
FileStream fs = new FileStream(strFilePath,FileMode.OpenorCreate,FileAccess.Read);
Response.WriteFile(strFilePath,fs.Length);
fs.Close();

第三种方法:
复制代码 代码如下:

string path=Server.MapPath("C#语言参考.doc");
FileInfo file=new FileInfo(path);
FileStream myfileStream=new FileStream(path,FileMode.Open,FileAccess.Read);
byte[] filedata=new Byte[file.Length];
myfileStream.Read(filedata,(int)(file.Length));
myfileStream.Close();
Response.Clear();
Response.ContentType="application/msword";
response.addheader("Content-disposition","attachment;filename=文件名.doc");
Response.Flush();
Response.BinaryWrite(filedata);
Response.End();

您可能感兴趣的文章:

C# 复制一个Word文档的部分或全部内容到另一个Word文档

C# 复制一个Word文档的部分或全部内容到另一个Word文档

C# 复制一个Word文档的部分或全部内容到另一个Word文档

我最近喜欢折腾Office软件相关的东西,想把很多Office软件提供的功能用.NET来实现,如果后期能把它用来开发一点我自己的小应用程序那就更好了。

扯远了,回到正题。复制文档内容这个功能太常见啦,在微软Word中实现这个功能很简单,只需要复制和粘贴就行了。这篇文章的主要目的是记录如何用C#来实现复制一个Word文档的部分或全部内容到另一个word文档,废话不多说,下面开始。

第一部分:复制部分内容:

在我的这个示例中,复制部分内容指的是复制一个word文档的部分段落(包括格式、图片和超链接等)到另一个word文档。

原文档截图:

                 

 

步骤参考:

第一步:新建一个word文档对象doc1并加载需要复制的word文档。

Document doc1 = new Document();
doc1.LoadFromFile("sample.docx");

 

第二步:新建一个word文档对象doc2。

Document doc2 = new Document();

 

第三步:获取被复制文档doc1的第一个section以及第一、二个段落(图片和标题)。

Section s = doc1.Sections[0];
Paragraph p1 = s.Paragraphs[0];
Paragraph p2 = s.Paragraphs[1];

 

第四步:给doc2添加一个section,并将doc1的第一二段的内容和格式等复制到doc2中。

Section s2 = doc2.AddSection();
Paragraph NewPara1 = (Paragraph)p1.Clone();
s2.Paragraphs.Add(NewPara1);
Paragraph NewPara2 = (Paragraph)p2.Clone();
s2.Paragraphs.Add(NewPara2);

 

第五步:保存并重新打开文档。

doc2.SavetoFile("copy.docx",FileFormat.Docx2010);
System.Diagnostics.Process.Start("copy.docx");

 

目标文档效果图:

 

 

第二部分:复制全部内容

复制全部内容指的是将除header和footer以外的其他所有内容复制到另一个文档。

步骤参考:

第一步:新建两个word document对象,并加载待复制的源word文档和目标word文档。

Document sourceDoc = new Document("sample.docx");
Document destinationDoc = new Document("target.docx");

 

第二步:遍历源word文档中的所有section并把它们的内容复制到目标word文档。

foreach (Section sec in sourceDoc.Sections)
{
    foreach (DocumentObject obj in sec.Body.Childobjects)
    {
        destinationDoc.Sections[0].Body.Childobjects.Add(obj.Clone());
    }
}

 

第三步:保存并重启目标word文档。

destinationDoc.SavetoFile("target.docx");
System.Diagnostics.Process.Start("target.docx");

 

在word文档之间单独复制header和footer也可以实现的,如有需要可以在下面留言。

原文档与效果图:

 

效果图:

 

 

结语:

文章写的比较简单,这里我还是使用了E-iceblue公司的免费Word控件,原因是它简单好用,而且我也使用的比较熟练了。接下来我还会研究它的其它功能,并用C#实现Office软件的其它功能。

C#实现合并多个word文档的方法

C#实现合并多个word文档的方法

本文实例讲述了C#实现合并多个word文档的方法,是非常具有实用价值的技巧。分享给大家供大家参考。

具体实现方法如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace driverexam.WordReport
{
  public class WordDocumentMerger
  {
    private ApplicationClass objApp = null;
    private Document objDocLast = null;
    private Document objDocBeforeLast = null;
    public WordDocumentMerger()
    {
      objApp = new ApplicationClass();
    }
    #region 打开文件
    private void Open(string tempDoc)
    {
      object objTempDoc = tempDoc;
      object objMissing = System.Reflection.Missing.Value;

   objDocLast = objApp.Documents.Open(
      ref objTempDoc,//FileName 
      ref objMissing,//ConfirmVersions 
      ref objMissing,//ReadOnly 
      ref objMissing,//AddToRecentFiles 
      ref objMissing,//PasswordDocument 
      ref objMissing,//PasswordTemplate 
      ref objMissing,//Revert 
      ref objMissing,//WritePasswordDocument 
      ref objMissing,//WritePasswordTemplate 
      ref objMissing,//Format 
      ref objMissing,//Enconding 
      ref objMissing,//Visible 
      ref objMissing,//OpenAndRepair 
      ref objMissing,//DocumentDirection 
      ref objMissing,//NoEncodingDialog 
      ref objMissing //XMLTransform 
      );
      objDocLast.Activate();
    }
    #endregion

    #region 保存文件到输出模板
    private void SaveAs(string outDoc)
    {
      object objMissing = System.Reflection.Missing.Value;
      object objOutDoc = outDoc;
      objDocLast.SaveAs(
      ref objOutDoc,//FileFormat 
      ref objMissing,//LockComments 
      ref objMissing,//PassWord 
      ref objMissing,//WritePassword 
      ref objMissing,//ReadOnlyRecommended 
      ref objMissing,//EmbedTrueTypeFonts 
      ref objMissing,//SaveNativePictureFormat 
      ref objMissing,//SaveFormsData 
      ref objMissing,//SaveAsAOCELetter,ref objMissing,//Encoding 
      ref objMissing,//InsertLineBreaks 
      ref objMissing,//AllowSubstitutions 
      ref objMissing,//LineEnding 
      ref objMissing //AddBiDiMarks 
      );
    }
    #endregion

    #region 循环合并多个文件(复制合并重复的文件)
    /// <summary> 
    /// 循环合并多个文件(复制合并重复的文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outDoc">合并后的输出文件</param> 
    public void copyMerge(string tempDoc,string[] arrcopies,string outDoc)
    {
      object objMissing = Missing.Value;
      object objFalse = false;
      object objTarget = WdMergeTarget.wdMergeTargetSelected;
      object objUseFormatFrom = WdUseFormattingFrom.wdFormattingFromSelected;
      try
      {
        //打开模板文件 
        Open(tempDoc);
        foreach (string strcopy in arrcopies)
        {
          objDocLast.Merge(
          strcopy,//FileName 
          ref objTarget,//MergeTarget 
          ref objMissing,//DetectFormatChanges 
          ref objUseFormatFrom,//UseFormattingFrom 
          ref objMissing //AddToRecentFiles 
          );
          objDocBeforeLast = objDocLast;
          objDocLast = objApp.ActiveDocument;
          if (objDocBeforeLast != null)
          {
            objDocBeforeLast.Close(
            ref objFalse,//SaveChanges 
            ref objMissing,//OriginalFormat 
            ref objMissing //RouteDocument 
            );
          }
        }
        //保存到输出文件 
        SaveAs(outDoc);
        foreach (Document objDocument in objApp.Documents)
        {
          objDocument.Close(
          ref objFalse,//SaveChanges 
          ref objMissing,//OriginalFormat 
          ref objMissing //RouteDocument 
          );
        }
      }
      finally
      {
        objApp.Quit(
        ref objMissing,//SaveChanges 
        ref objMissing,//OriginalFormat 
        ref objMissing //RoutDocument 
        );
        objApp = null;
      }
    }
    /// <summary> 
    /// 循环合并多个文件(复制合并重复的文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outDoc">合并后的输出文件</param> 
    public void copyMerge(string tempDoc,string strcopyFolder,string outDoc)
    {
      string[] arrFiles = Directory.GetFiles(strcopyFolder);
      copyMerge(tempDoc,arrFiles,outDoc);
    }
    #endregion

    #region 循环合并多个文件(插入合并文件)
    /// <summary> 
    /// 循环合并多个文件(插入合并文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outDoc">合并后的输出文件</param> 
    public void InsertMerge(string tempDoc,string outDoc)
    {
      object objMissing = Missing.Value;
      object objFalse = false;
      object confirmConversion = false;
      object link = false;
      object attachment = false;
      try
      {
        //打开模板文件 
        Open(tempDoc);
        foreach (string strcopy in arrcopies)
        {
          objApp.Selection.InsertFile(
          strcopy,ref confirmConversion,ref link,ref attachment
          );
        }
        //保存到输出文件 
        SaveAs(outDoc);
        foreach (Document objDocument in objApp.Documents)
        {
          objDocument.Close(
          ref objFalse,//OriginalFormat 
        ref objMissing //RoutDocument 
        );
        objApp = null;
      }
    }
    /// <summary> 
    /// 循环合并多个文件(插入合并文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outDoc">合并后的输出文件</param> 
    public void InsertMerge(string tempDoc,string outDoc)
    {
      string[] arrFiles = Directory.GetFiles(strcopyFolder);
      InsertMerge(tempDoc,outDoc);
    }
    #endregion
  }
}

相信本文所述对大家的C#程序设计有一定的借鉴价值。

C#实现简单合并word文档的方法

C#实现简单合并word文档的方法

本文实例讲述了C#实现简单合并word文档的方法。分享给大家供大家参考。具体如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Demo
{
 public partial class Form2 : Form
 {
  public Form2()
  {
   InitializeComponent();
  }
  string path = @"C:\Documents and Settings\Administrator\桌面\output.doc";
  string add = @"C:\Documents and Settings\Administrator\桌面\file";
  private Microsoft.Office.Interop.Word.ApplicationClass applicationClass;
  private Microsoft.Office.Interop.Word.Document doc;
  private void button1_Click(object sender,EventArgs e)
  {
   Ex();
  }
  void Ex()
  {
   Open(path);
   string[] files = System.IO.Directory.GetFiles(add);
   foreach (string s in files)
   {
    InsertFile(s);
   }
   SaveAs(path);
  }
  /// <summary>
  /// 打开输出word文档
  /// </summary>
  /// <param name="strFileName"></param>
  public void Open(string strFileName)
  {
   applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
   object fileName = strFileName;
   object readOnly = false;
   object isVisible = true;
   object missing = System.Reflection.Missing.Value;
   doc = applicationClass.Documents.Open(ref fileName,ref missing,ref missing);
   doc.Activate();
  }
  /// <summary>
  /// 向打开的word文档中插入word文档
  /// </summary>
  /// <param name="strFileName"></param>
  public void InsertFile(string strFileName)
  {
   object missing = System.Reflection.Missing.Value;
   object confirmConversion = false;
   object link = false;
   object attachment = false;
   applicationClass.Selection.InsertFile(strFileName,ref confirmConversion,ref link,ref attachment);
   object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
   applicationClass.Selection.InsertBreak(ref pBreak);
  }
  /// <summary>
  /// 最后保存word文档
  /// </summary>
  /// <param name="strFileName"></param>
  public void SaveAs(string strFileName)
  {
   object missing = System.Reflection.Missing.Value;
   object fileName = strFileName;
   doc.SaveAs(ref fileName,ref missing);
  }
 }
}

@H_301_4@

希望本文所述对大家的C#程序设计有所帮助。

关于jsp实现针对excel及word文档的打印方法用jsp方式打印出6×6表格的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于asp.net(c#)下读取word文档的方法小结、C# 复制一个Word文档的部分或全部内容到另一个Word文档、C#实现合并多个word文档的方法、C#实现简单合并word文档的方法等相关知识的信息别忘了在本站进行查找喔。

本文标签:

上一篇JSP实现的简单分页显示效果代码(jsp实现的简单分页显示效果代码是什么)

下一篇在Go语言程序中使用gojson来解析JSON格式文件(go语言 json解析)