首页 技术 正文
技术 2022年11月18日
0 收藏 404 点赞 2,873 浏览 3161 个字

protected void btgua_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables[0];
if (dt != null)
{
#region 操作excel
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
xlWorkBook = new Microsoft.Office.Interop.Excel.Application().Workbooks.Add(Type.Missing);
xlWorkBook.Application.Visible = false;
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];

//设置标题

xlWorkSheet.Cells[1, 1] = “发送内容”;
xlWorkSheet.Cells[1, 2] = “发送时间”;
xlWorkSheet.Cells[1, 3] = “发送图片”;
//设置宽度
((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 2]).ColumnWidth = 15;
//设置字体
xlWorkSheet.Cells.Font.Size = 12;
#region 为excel赋值
for (int i = 0; i < dt.Rows.Count; i++)
{
//为单元格赋值。
xlWorkSheet.Cells[i + 2, 1] = dt.Rows[i][“SendInfo”].ToString();
xlWorkSheet.Cells[i + 2, 2] = dt.Rows[i][“SendTime”].ToString();
#region

//可以直接取图片的地址
if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i][“Temp1”].ToString()))
{
string filename = Server.MapPath(“upload/nianhuo/”) + dt.Rows[i][“Temp1”].ToString();
#endregion
int rangeindex = i + 2;
string rangename = “C” + rangeindex;

Microsoft.Office.Interop.Excel.Range range = xlWorkSheet.get_Range(rangename, Type.Missing);

range.Select();
/////////////
float PicLeft, PicTop, PicWidth, PicHeight;    //距离左边距离,顶部距离,图片宽度、高度
PicTop = Convert.ToSingle(range.Top);
PicWidth = Convert.ToSingle(range.MergeArea.Width);
PicHeight = Convert.ToSingle(range.Height);
PicWidth = Convert.ToSingle(range.Width);
PicLeft = Convert.ToSingle(range.Left);
////////////////////

Microsoft.Office.Interop.Excel.Pictures pict = (Microsoft.Office.Interop.Excel.Pictures)xlWorkSheet.Pictures(Type.Missing);
if (filename.IndexOf(“.”) > 0)
{
if (System.IO.File.Exists(filename))
{
// pict.Insert(filename, Type.Missing);//显示原图 重叠在一起
xlWorkSheet.Shapes.AddPicture(filename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, PicLeft, PicTop, PicWidth, PicHeight);//指定位置显示小图
}
}
}
// ActiveWorkbook

}
#endregion
#region 保存excel文件
Random myrand= new Random();
string filenamess = System.DateTime.Now.ToString(“yyyyMMddhhMMss”) + myrand.Next(0, 100);
string filePath = Server.MapPath(“excel”)+@”\” +filenamess+”.xls”;
xlWorkBook.SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlWorkBook.Application.Quit();
xlWorkSheet = null;
xlWorkBook = null;
GC.Collect();
System.GC.WaitForPendingFinalizers();
#endregion
#endregion

#region 导出到客户端
Response.ContentEncoding = System.Text.Encoding.GetEncoding(“GB2312”);
Response.AppendHeader(“content-disposition”, “attachment;filename=” + System.Web.HttpUtility.UrlEncode(filenamess, System.Text.Encoding.UTF8) + “.xls”);
Response.ContentType = “Application/excel”;
Response.WriteFile(filePath);
Response.End();
#endregion
KillProcessexcel(“EXCEL”);

}

}
#region 杀死进程
private void KillProcessexcel(string processName)
{ //获得进程对象,以用来操作
System.Diagnostics.Process myproc = new System.Diagnostics.Process();
//得到所有打开的进程
try
{
//获得需要杀死的进程名
foreach (Process thisproc in Process.GetProcessesByName(processName))
{ //立即杀死进程
thisproc.Kill();
}
}
catch (Exception Exc)
{
throw new Exception(“”, Exc);
}
}
#endregion

using System.Reflection;
using System.Diagnostics;

using Microsoft.Office.Interop.Excel;

Using Office;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919