C#代码怎么将数据库中的数据导成DOC和PDF格式

C#代码怎么将数据库中的数据导成DOC和PDF格式,第1张

微软从未公开过doc文件格式信息,所以如果你想将文本保存为doc格式,只有 *** 作word完成。如何开发基于Word的net程序你在网上搜索以下,很多。 至于PDF,给你个简单的例子:using System;

using SystemIO;

using SystemText;

using SystemCollections;namespace PDFGenerator

{

/// <summary>

/// Application : Generation of PDF file from text

/// Author : Pramod Kumar Singh

/// Date : 25th July 2001

///</summary>public class PDFGenerator

{

static float pageWidth = 5940f;

static float pageDepth = 8280f;

static float pageMargin = 300f;

static float fontSize = 100f;

static float leadSize = 100f;//Create a PDF file

//PDF on Disk

static StreamWriter pPDF=new StreamWriter("F:\\Temp\\myPDFpdf");

//PDF in Memory

static MemoryStream mPDF= new MemoryStream();//Convert the Text Data to PDF format and write back to

//Memory Stream

static void ConvertToByteAndAddtoStream(string strMsg)

{

Byte[] buffer=null;

buffer=ASCIIEncodingASCIIGetBytes(strMsg);

mPDFWrite(buffer,0,bufferLength);

buffer=null;

}//Format the data length in xRef Format

static string xRefFormatting(long xValue)

{

string strMsg =xValueToString();

int iLen=strMsgLength;

if (iLen<10)

{

StringBuilder s=new StringBuilder();

//string s=null;

int i=10-iLen;

sAppend('0',i);

strMsg=sToString() + strMsg;

}

return strMsg;

}//Entry Point

static void Main(string[] args)

{

//Create a ArrayList for xRefs of PDF Document

ArrayList xRefs=new ArrayList();

Byte[] buffer=null;

float yPos =0f;

long streamStart=0;

long streamEnd=0;

long streamLen =0;

string strPDFMessage=null;

//PDF Header Message

strPDFMessage="%PDF-11\n";

ConvertToByteAndAddtoStream(strPDFMessage);//ID 1 For Containt

//ID 2 For Length of the Stream

//write the Text//1> Start a new Page

xRefsAdd(mPDFLength);

strPDFMessage="1 0 obj\n";

ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage="<< /Length 2 0 R >>\n";

ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage="stream\n";

ConvertToByteAndAddtoStream(strPDFMessage);//Get the start of the stream

streamStart=mPDFLength;

strPDFMessage="BT\n/F0 " + fontSize +" Tf\n";

ConvertToByteAndAddtoStream(strPDFMessage);

yPos = pageDepth - pageMargin;

strPDFMessage=pageMargin + " " + yPos +" Td\n" ;

ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage= leadSize+" TL\n" ;

ConvertToByteAndAddtoStream(strPDFMessage);//Add the text data to the PDF memory stream

strPDFMessage= "(Pramod Kumar Singh)Tj\n" ;

ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage= "ET\n";

ConvertToByteAndAddtoStream(strPDFMessage);

//Get the End of the stream

streamEnd=mPDFLength;

//Get the Length of the stream

streamLen=streamEnd-streamStart;

strPDFMessage= "endstream\nendobj\n";

ConvertToByteAndAddtoStream(strPDFMessage);//Add 2 object to xRef

xRefsAdd(mPDFLength);

strPDFMessage="2 0 obj\n"+ streamLen + "\nendobj\n";

ConvertToByteAndAddtoStream(strPDFMessage);//Add Page to xRefs

xRefsAdd(mPDFLength);

strPDFMessage="3 0 obj\n<</Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";

ConvertToByteAndAddtoStream(strPDFMessage);//Build the Pages

xRefsAdd(mPDFLength);

strPDFMessage="4 0 obj\n<</Type /Pages /Count 1\n";

ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage="/Kids[\n3 0 R\n]\n";

ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage="/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >>\n";

ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage="/MediaBox [ 0 0 "+ pageWidth + " " + pageDepth + " ]\n>>\nendobj\n";

ConvertToByteAndAddtoStream(strPDFMessage);//Add font to xRefs

xRefsAdd(mPDFLength);

strPDFMessage="5 0 obj\n<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";

ConvertToByteAndAddtoStream(strPDFMessage);//Add the catalog to xRefs

xRefsAdd(mPDFLength);

strPDFMessage="6 0 obj\n<</Type/Catalog/Pages 4 0 R>>\nendobj\n";

ConvertToByteAndAddtoStream(strPDFMessage);//xRefs Entry

streamStart=mPDFLength;

strPDFMessage="xref\n0 7\n0000000000 65535 f \n";

for(int i=0;i<xRefsCount;i++)

{

strPDFMessage+=xRefFormatting((long) xRefs[i])+" 00000 n \n";

}

ConvertToByteAndAddtoStream(strPDFMessage);

//Trailer for the PDF

strPDFMessage="trailer\n<<\n/Size "+ (xRefsCount+1)+"\n/Root 6 0 R\n>>\n";

ConvertToByteAndAddtoStream(strPDFMessage);

//xRef location entry

strPDFMessage="startxref\n" + streamStart+"\n%%EOF\n";

ConvertToByteAndAddtoStream(strPDFMessage);

//Write the PDF from Memory Stream to File Stream

mPDFWriteTo(pPDFBaseStream);

//Close the Stream

mPDFClose();

pPDFClose();

}

}

}

'如何打开并输出一个数据库中的文件流?

'按普通方式打开数据库,之后求得该文件字节大小,在网页中输出该文件MIME类型,再用binarywrite输出

'之前必须将以下语句放在一个页面中,而在另一个页面对之进行调用输出显示。

Set rs = ServerCreateObject("ADODBrecordset")

sql="select from b_hn_sgbb_rtu where sgbh='02713700001' and xh='2'"

rsopen sql,cn,1,1

'以下两句是在网页中打开WORD文档

'tu_size=rs("tu")ActualSize

'ResponseContentType = rs("types")

'ResponseBinaryWrite rs("tu")getChunk(tu_size)

'以下三句是将数据库中的流文件当作一个文件,可以打开也可以下载回来

ResponseContentType = "Application/octet-stream"

tu_size=rs("tu")ActualSize

ResponseAddHeader "Content-Disposition", "attachment; filename=" & rs("filename")

ResponseBinaryWrite rs("tu")getChunk(tu_size)

rsclose

'常见文件的MIME类型

'GIF文件 "image/gif"

'BMP文件 "image/bmp"

'JPG文件 "image/jpeg"

'zip文件 "application/x-zip-compressed"

'DOC文件 "application/msword"

'文本文件 "text/plain"

'HTML文件 "text/html"

'一般文件 "application/octet-stream"

从数据库中读取存储了与用户有关的资料,然后把这些资料放到一个以用户ID(userid)命名的文件夹中,再在这个文件夹里创建一个useriddoc文件,用于存放取到的用户资料,用户资料包括文字类型、。

数据库是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计算机内的、有组织的、可共享的、统一管理的大量数据的集合。

数据库是一个按数据结构来存储和管理数据的计算机软件系统。数据库的概念实际包括两层意思:数据库是一个实体,它是能够合理保管数据的“仓库”,用户在该“仓库”中存放要管理的事务数据,“数据”和“库”两个概念结合成为数据库。

数据库系统,是由数据库及其管理软件组成的系统。数据库系统是为适应数据处理的需要而发展起来的一种较为理想的数据处理系统,也是一个为实际可运行的存储、维护和应用系统提供数据的软件系统,是存储介质 、处理对象和管理系统的集合体。

以上就是关于C#代码怎么将数据库中的数据导成DOC和PDF格式全部的内容,包括:C#代码怎么将数据库中的数据导成DOC和PDF格式、把zip或doc文件存到了SQL Server的数据库中,怎样把它读出、php 如何把数据库中的记录 写入到word 中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9841366.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存