文件导出
如何把数据库文件导出.docx/.doc/.rtf文件中,数据困里面有图片和图片一起导出
--------------------编程问答--------------------
如果是在访问页面的时候做这个导出的功能,我想,肯定是有难度的,楼主你可以写个windows服务,对数据中的记录都在服务器端生成一个对应的word文件,然后下载的时候,指向这个生成的文件就行了。
--------------------编程问答--------------------
//先添加相关的引用,直接在添加引用里就能找到这些dll:
//导入Com组件
using Microsoft.Office.Core;
//添加引用
using Excel = Microsoft.Office.Interop.Excel;
导出方法:
#region ExportData
/// <summary>
/// 导出数据到Excel(包括图片)
/// </summary>
private void ExportData()
{
object missing = System.Reflection.Missing.Value;
// 定义一个Excel应用程序
Excel.Application excel = new Excel.Application();
if (excel == null)
{
return;
}
Excel.Workbooks workbooks = excel.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
Excel.Range range = null;
worksheet.Cells[1, 1] = "导出的Excel数据"; //标题
range = (Excel.Range)worksheet.Cells[1, 1];
range.Font.Bold = true;// 字体加粗
range.Font.Size = 18; // 设置字体大小
//range.Interior.Color = 160; // 根据索引设置颜色
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;// 设置文本居中
range = (Excel.Range)worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 13]);// 选择区域(标题区域)
range.Select();// 选中区域
range.Merge(false);// 合并单元格
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
// 添加图片
range = (Excel.Range)worksheet.get_Range("A20", missing);// 获取区域
range.Select();// 选中区域
// 声明一个Pictures对象,用来保存图片
Excel.Pictures pics = (Excel.Pictures)worksheet.Pictures(missing);
// 插入图片
pics.Insert(Server.MapPath("Images/Excel图片.jpg"), missing);// 在选中区域插入图片
// 输出表头
worksheet.Cells[3, 1] = "列名称1";
worksheet.Cells[3, 2] = "列名称2";
worksheet.Cells[3, 3] = "列名称3";
worksheet.Cells[3, 4] = "列名称4";
worksheet.Cells[3, 5] = "列名称5";
worksheet.Cells[3, 6] = "列名称6";
worksheet.Cells[3, 7] = "列名称7";
worksheet.Cells[3, 8] = "列名称8";
worksheet.Cells[3, 9] = "列名称9";
worksheet.Cells[3, 10] = "列名称10";
worksheet.Cells[3, 11] = "列名称11";
worksheet.Cells[3, 12] = "列名称12";
worksheet.Cells[3, 13] = "列名称13";
// 输出内容
//int flag = 3;
//foreach (var w in list)
//{
// flag += 1;
// worksheet.Cells[flag, 1] = w.One;
// worksheet.Cells[flag, 2] = w.Two;
// worksheet.Cells[flag, 3] = w.Three;
// worksheet.Cells[flag, 4] = w.Four;
// worksheet.Cells[flag, 5] = w.Five;
// worksheet.Cells[flag, 6] = w.Six;
// worksheet.Cells[flag, 7] = w.Seven;
// worksheet.Cells[flag, 8] = w.Eight;
// worksheet.Cells[flag, 9] = w.Nine;
// worksheet.Cells[flag, 10] = w.Ten;
// worksheet.Cells[flag, 11] = w.Eleven;
// worksheet.Cells[flag, 12] = w.Twelve;
// worksheet.Cells[flag, 13] = w.Thirteen;
//}
string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".xls";
string filePath = Server.MapPath("Files/") + fileName;
try
{
workbook.Saved = true;
workbook.SaveCopyAs(filePath);
}
catch (Exception ex)
{
throw (ex);
}
// 必须关闭释放所引用的COM对象,关闭Excel进程,否则会占用服务器资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
workbook.Close(false, false, missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
// 强制垃圾回收
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
// 提供下载
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
file.Delete();
Response.Clear();
Response.Charset = "UTF-8";
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
// 指定文件类型
Response.ContentType = "Application/ms-excel";
// 把文件流发送到客户端
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();
// 停止该页的执行
Response.End();
}
else
{
Response.Write("<script>alert('文件不存在!')</script>");
Response.End();
}
}
#endregion
//里面的输出内容,自己绑定要读取的数据就行。
补充:.NET技术 , ASP.NET