62,267
社区成员
发帖
与我相关
我的任务
分享
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (ck.Checked)
{
string id = GridView1.Rows[i].Cells[1].Text.Trim();
string title = GridView1.Rows[i].Cells[2].Text.Trim();
Export("application/ms-excel", "excel.xls");
}
}
}
private void Export(string FileType, string FileName)
{
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//修改下:
<input type="button" onclick="AutomateExcel()" value="导出" style="width: 41px" class="button">
<SCRIPT LANGUAGE="JavaScript">
function AutomateExcel()
{
try {
var oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert('您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”!');
return '';
}
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.getElementById("表格名称");
var hang = table.rows.length;
if(hang>0)
{
var lie = table.rows(0).cells.length;
for (i=0;i<hang;i++)
{
//假如你的checkbox在第1列
var cb = table.rows[i].cells[0].getElementsTagName("input");
if(cb!=null && cb.type="checkbox" && cb.checked)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).NumberFormatLocal="@";
oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
}
}
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
<input type="button" onclick="AutomateExcel()" value="导出" style="width: 41px" class="button">
<SCRIPT LANGUAGE="JavaScript">
function AutomateExcel()
{
try {
var oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert('您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”!');
return '';
}
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.getElementById("表格名称");
var hang = table.rows.length;
if(hang>0)
{
var lie = table.rows(0).cells.length;
for (i=0;i<hang;i++)
{
//假如你的checkbox在第1列
var cb = table.rows[i].cells[0].getElementsTagName("checkbox");
if(cb!=null && cb.checked)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).NumberFormatLocal="@";
oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
}
}
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
</SCRIPT>
5楼的方法可行,还有一种可以通过js来实现导出:
<SCRIPT LANGUAGE="JavaScript">
function AutomateExcel()
{
try {
var oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert('您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”!');
return '';
}
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.getElementById("Excel");
var hang = table.rows.length;
if(hang>0)
{
var lie = table.rows(0).cells.length;
for (i=0;i<hang;i++)
{
//这里判断checkbox是否选中,假如checkbox在第1列
var cb = table.rows[i].cells[0].getElementsTagName("checkbox");
if(cb!=null && cb.checked)
{
for (j=0;j<lie;j++)
{
oSheet.Cells(i+1,j+1).NumberFormatLocal="@";
oSheet.Cells(i+1,j+1).Value = table.rows(i).cells(j).innerText;
}
}
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
</SCRIPT>
//页面调用js
<input type="button" onclick="AutomateExcel()" value="导出" style="width: 41px" class="button">
protected void Button2_Click(object sender, EventArgs e)
{
StringBuilder s = new StringBuilder();
s.AppendLine("<table style='width:800px' border=1>");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (ck.Checked)
{
//GridView1.Columns
s.AppendLine("<tr>");
for (int j = 0; j < GridView1.Columns.Count; j++)
{
s.AppendFormat("<td>{0}</td>", GridView1.Rows[i].Cells[j].Text);
}
s.AppendLine("</tr>");
}
}
s.Append("</table>");
Export("application/ms-excel", "excel.xls",s);
}
private void Export(string FileType, string FileName,string s)
{
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
Response.Write(s);
Response.End();
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (ck.Checked)
{
string id = GridView1.Rows[i].Cells[1].Text.Trim();
string title = GridView1.Rows[i].Cells[2].Text.Trim();
Export("application/ms-excel", "excel.xls",title);
}
}
}
private void Export(string FileType, string FileName,string title)
{
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
Response.Write(title);
Response.End();
}
没试! 你自己试试! 还有把你的情况说清楚点..........
是只导出 选种的行吗? 如果是的话!那你直接把选种的行的数据传递到Export 以流的方式保存就好了.