asp.net利用剪切板导出excel
public enum ClipboardFormats : uint
{
CF_TEXT = 1,
CF_BITMAP = 2,
CF_METAFILEPICT = 3,
CF_SYLK = 4,
CF_DIF = 5,
CF_TIFF = 6,
CF_OEMTEXT = 7,
CF_DIB = 8,
CF_PALETTE = 9,
CF_PENDATA = 10,
CF_RIFF = 11,
CF_WAVE = 12,
CF_UNICODETEXT = 13,
CF_ENHMETAFILE = 14
} public class ClipboardUtility
{
[DllImport("user32.dll")]
private static extern bool OpenClipboard(IntPtr hWndNewOwner); [DllImport("user32.dll")]
private static extern bool EmptyClipboard(); [DllImport("user32.dll")]
private static extern IntPtr GetClipboardData(uint uFormat); [DllImport("user32.dll")]
private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); [DllImport("user32.dll")]
private static extern bool CloseClipboard(); [DllImport("kernel32.dll")]
private static extern UIntPtr GlobalSize(IntPtr hMem); public static void SetClipboardText(string text, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(text);
IntPtr alloc = Marshal.AllocHGlobal(bytes.Length + 1);
Marshal.Copy(bytes, 0, alloc, bytes.Length);
OpenClipboard(IntPtr.Zero);
EmptyClipboard();
SetClipboardData((uint)ClipboardFormats.CF_TEXT, alloc);
CloseClipboard();
} public static string GetClipboardText(Encoding encoding)
{
OpenClipboard(IntPtr.Zero);
IntPtr alloc = GetClipboardData((uint)ClipboardFormats.CF_TEXT);
byte[] bytes = new byte[(int)GlobalSize(alloc)];
Marshal.Copy(alloc, bytes, 0, bytes.Length);
CloseClipboard();
return encoding.GetString(bytes);
}
[STAThread]
public static void clear()
{
Clipboard.Clear();
}
} public bool CreateExcelFileForDataTable()
{
Excel.Application app = new Excel.Application();
app.Visible = true;//让Excel显示(调试用)
Excel.Workbooks ws = app.Workbooks;
Excel.Workbook workbook = ws.Add(Excel.XlWBATemplate.xlWBATWorksheet); // 默认已经创建了一个worksheet
int sheetCount = 2;//Excel页数
workbook.Sheets.Add(Type.Missing, workbook.Sheets[1], sheetCount, Type.Missing);
List<StringBuilder> list = new List<StringBuilder>();
StringBuilder sb = new StringBuilder();
sb.Append(@"<table align='center' border='0' cellpadding='2' cellspacing='0' width='100%'>
<tbody>
<tr>
<td style='color: #ff0000'><asp:Label ID='lblTX0' runat='Server' >WT</asp:Label><br /></td>
<td style='color: #ff0000'><asp:Label ID='lblTX0' runat='Server' >WT</asp:Label><br /></td>
</tr>
<tr>
<td style='color: #ff0000'><asp:Label ID='lblTX0' runat='Server' >WT</asp:Label><br /></td>
<td style='color: #ff0000'><asp:Label ID='lblTX0' runat='Server' >WT</asp:Label><br /></td>
</tr>
</tbody>
</table>");
list.Add(sb);
list.Add(sb);
list.Add(sb);
for (int i = 1; i <= workbook.Sheets.Count; i++)
{ Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[i]; //得到worksheet
worksheet.Name = "第" + i.ToString() + "个Sheet";// 为worksheet设置名字 ClipboardUtility.SetClipboardText(list[i - 1].ToString(),Encoding.Default);//用默认编码设置剪贴板内容
worksheet.Paste();//从剪贴板粘贴到Excel中。
worksheet.Columns.EntireColumn.AutoFit(); //自动适应长度
}
//如果Excel不显示,记得最后要关闭Excel,不然会开很多在内存。
ws.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null; return true;
}
asp.net利用剪切板导出excel的更多相关文章
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- android利用剪切板来实现数据的传递
在Android开发中我们经常要遇到的一个问题就是数据在不同的Activity之间的共享.在Android开发中有很多种方法可以达到这个目地. 这里介绍一种比较常见.又常用的一种方法就是使用剪切板.我 ...
- Android 利用剪切板(clipboardManager )实现数据传递
首先是系统剪切板的调用服务: ClipboardManager ClipboardManager=getSystemService(Context.CLIPBOARD_SERVICE); 然后是写入, ...
- ASP.Net MVC利用NPOI导入导出Excel
因近期项目遇到所以记录一下: 首先导出Excel: 首先引用NPOI包 http://pan.baidu.com/s/1i3Fosux (Action一定要用FileResult) /// <s ...
- 利用POi3.8导出excel产生大量xml临时文件怎么办?
在实际项目中,经常会用到POI3.8来导出excel.而导出excel的时候,会因为残留大量以.xml结尾的文件而导致服务器存储空间急剧增长,最后导致系统挂了.为此,该怎么办呢? .xml后缀残留文件 ...
- iOS利用剪切板在app中传递信息
利用iOS剪切板在app中传递信息 App1 中添加URLSchemes app1 App2 中国添加URLSchemes app2 App1中进入app2: UIApplication.sh ...
- .net mvc利用NPOI导入导出excel
1.导出Excel :首先引用NPOI包(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> ...
- ASP.NET MVC 使用NPOI导出Excel 无法访问已关闭的流(转)
第一步重写MemoryStream , 让它不能自动关闭. //新建类 重写Npoi流方法 public class NpoiMemoryStream : MemoryStream { public ...
- net mvc 利用NPOI导入导出excel
1.导出Excel : 首先引用NPOI包(Action一定要用FileResult) /// <summary> /// 批量导出需要导出的列表 /// </summary> ...
随机推荐
- codeforces 451E. Devu and Flowers 容斥原理+lucas
题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...
- php文件链接数据库基本代码
<?php $conn=@mysql_connect("localhost","root",""); if($conn==null) ...
- 转 释一首美国民谣:沉默之音(The Sound Of Silence)
Ask not what your country can do for you , ask what you can do for your country. 六十年代对美国而言是个多事之秋的 ...
- android中使用setVideoURI()播放视频
最近在做一个demo,要求播放视频,记录一下.使用的是VideoView控件,如果播放网络视频的话,视频应该是渐进流式的,格式嘛,大家应该都知道,一般是H.263或者H.264格式的扩展名为3gp或者 ...
- Windows Azure HDInsight 支持预览版 Hadoop 2.2 群集
Windows Azure HDInsight 支持预览版 Hadoop 2.2 群集 继去年 10 月推出 Windows Azure HDInsight 之后,我们宣布 Windows Az ...
- 宣布正式发布 Windows Azure 多重身份验证
身份和访问管理是安全之锚,是企业 IT 部门的首要任务.它是随时随地向员工.合作伙伴和客户提供访问的关键所在.今天,我们非常高兴地宣布正式发布 Windows Azure 多重身份验证,从而为 IT ...
- Poj 1166 The Clocks(bfs)
题目链接:http://poj.org/problem?id=1166 思路分析:题目要求求出一个最短的操作序列来使所有的clock为0,所以使用bfs: <1>被搜索结点的父子关系的组织 ...
- http://blog.csdn.net/baimafujinji/article/details/10931621
书接上文,本文章是该系列的第二篇,按照总纲中给出的框架,本节介绍三个中值定理,包括它们的证明及几何意义.这三个中值定理是高等数学中非常基础的部分,如果读者对于高数的内容已经非常了解,大可跳过此部分.当 ...
- Thrift对多接口服务的支持
Thrift对多接口服务的支持 Thrift在0.9.1版本之前,一直只提交了对单一接口服务的支持,即一个RPC服务器(对应一个端口)支持一个服务接口的实现. 但是很多时候,我们的服务不能实现在一个接 ...
- redis研究笔记
本文链接:http://blog.csdn.net/u012150179/article/details/38077851 一. redis Redis is an in-memory databas ...