简单明了 c#web 导出 excel,word,pdf, 只包含了文本导出。
来源:互联网 发布:淘宝发货地不一样 编辑:程序博客网 时间:2024/06/05 17:10
其中pdf需要 itextSharp库
public static string sGetBinTemplaterFile(string sfilemode)
{
return System.Web.HttpContext.Current.Server.MapPath("bin\\templater." + sfilemode);
}
public static void DataSetToExcel(string strText)
{
//excel
string filepath = sGetBinTemplaterFile("xlsx");
HttpResponse resp;
resp = HttpContext.Current.Response;
resp.Clear();
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + filepath);
resp.Write(strText);
resp.Flush();
resp.End();
}
public static void DataSetToWord(string strText)
{
string filepath = sGetBinTemplaterFile("doc");
HttpResponse resp;
resp = HttpContext.Current.Response;
resp.Clear();
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + filepath);
resp.Write(strText);
resp.Flush();
resp.End();
}
public static void DataSetToPdf(string strText)
{
string filepath = sGetBinTemplaterFile("pdf");
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
byte[] buffer = new byte[fs.Length];
fs.Position = 0;
fs.Read(buffer, 0, (int)fs.Length);
HttpResponse resp;
resp = HttpContext.Current.Response;
resp.Clear();
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + filepath);
resp.Clear();
resp.AddHeader("Content-Length", fs.Length.ToString());
resp.ContentType = "application/pdf";
resp.AddHeader("Content-Disposition", "inline;FileName=out.pdf");
fs.Close();
resp.BinaryWrite(buffer);
resp.OutputStream.Flush();
resp.OutputStream.Close();
//pdf itextsharp
// string file1 = "E:\\test.pdf";
// Document document = new Document();
// FileStream fs = System.IO.File.Open(file1, FileMode.OpenOrCreate);
//
// PdfWriter.GetInstance(document, fs);
//
// document.Open();
// BaseFont bfChinese = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// Font fontChinese = new Font(bfChinese, 12);
//
// Table aTable = new Table(1);
// aTable.Border = 0;
// aTable.AddCell(new Paragraph("a1\tb1\tc1\na2\tb2\tc2\n ", fontChinese));
// document.Add(new Paragraph("a1\tb1\tc1\na2\tb2\tc2\n ", fontChinese));
//
//
// //document.Add(aTable);
//
// document.Close();
}
public static string GetDataToString(DataSet ds)
{
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
string colHeaders = "", ls_item = "", strText = "";
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加\n
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
strText += colHeaders;
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加\n
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
strText += ls_item;
ls_item = "";
}
return strText;
}
- 简单明了 c#web 导出 excel,word,pdf, 只包含了文本导出。
- 导出EXCEL,PDF 方法简单明了
- 水晶报表导出为pdf, word , excel 格式(已经测试过,实现了该功能)
- WEB页面导出为EXCEL,Word
- iText导出word、pdf
- 导出excel、pdf
- 水晶报表导出为word,excel和pdf格式
- 水晶报表导出为word,excel和pdf格式
- 实战应用导出gridview到excel word pdf保留格式
- 实战应用导出gridview到excel word pdf保留格式
- ireport导出各种格式(pdf,excel,word,html,print)
- ireport导出各种格式(pdf,excel,word,html,print)
- datatable导出excel、word、png、pdf等样式
- C# word或者excel保存或导出为PDF
- vs2005导出excel、word
- 导出Word、Excel报表
- 导出WORD,EXCEL
- jsp 导出 word excel
- Java学习笔记----------集合Set
- 粗解hibernate
- 数据库如何创建一个完全一样的表
- java中的IO整理
- WinExec(用来打开指定exe文件)
- 简单明了 c#web 导出 excel,word,pdf, 只包含了文本导出。
- Action bar
- Swing 透明窗体实现
- Discuz 可以进主页,但是进入点击版块和帖子信息的时候就会出现http404错误
- ORA-12154: TNS: 无法解析指定的连接标识符---原来是tnsnames.ora多了空格
- ActionMode
- Code Review:代码审查工具大阅兵
- 第一章 Java概述、安装和配置
- 自定义 Action Bar