easyui datadgrid导出excel 出错无效值(续:C#中,dataGridView的数据导出到Excel时,部分数据错误!)

本文目录
- 续:C#中,dataGridView的数据导出到Excel时,部分数据错误!
- 各位大神!请问导出excel 打开时提示文件格式无效怎么解决
- easyui表格方法$(’#dev_data’).datagrid({rowStyler: function(index,row){return ’font-size:10;’;}无效
- java导出excel,excel打不开,报文件格式无效,怎么解决!
- 发现在easyui datagrid getSelections一个小bug,怎么解决
- easyui datagrid 获取不到值
- 请高手指明:gridview的数据到导出Excel时,没有数据(为空白内容)
- excel公式选定的范围内有无效值怎么办
续:C#中,dataGridView的数据导出到Excel时,部分数据错误!
可以。其实在书本的例子也是有源代码的
首先肯定是先添加引用,是excel的引用哦
然后就是代码部分,button3(导出按钮)的click事件为:
private void button3_Click(object sender, EventArgs e)
{
ExportDataGridview(dataGridView1, true);
}
public bool ExportDataGridview(DataGridView dgv, bool isShowExcle)
{
try
{
if (dgv.Rows.Count == 0)
return false;
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
for (int i = 0; i 《 dgv.ColumnCount; i++)
{
excel.Cells.HeaderText;
}
for (int i = 0; i 《 dgv.RowCount - 1; i++)
{
for (int j = 0; j 《 dgv.ColumnCount; j++)
{
if (dgv.ValueType == typeof(string))
{
excel.Cells.Value.ToString();
}
else
{
excel.Cells.Value.ToString();
}
}
}
}
catch
{
}
return true;
}
其实最主要的是你有安装完整版的Microsoft Office
各位大神!请问导出excel 打开时提示文件格式无效怎么解决
1、该文件被破坏,文件损坏一般不可恢复。可以重新下载。但是有时候下载的时候会下载不全导致损坏
2、该文件版本跟你当前的excel版本不兼容。比如文件版本较高。
easyui表格方法$(’#dev_data’).datagrid({rowStyler: function(index,row){return ’font-size:10;’;}无效
你可以F12或用Firefox的firebug 查看页面元素, 可以发现datagrid的单元格所在的div有个datagrid-cell样式, 可以查看easyui.css 1196行(version 1.4),这个样式里面有个font-size: 12px; 所以通过rowStyle给行添加的样式只是datagrid-cell 样式的补充 而不能替换 。
java导出excel,excel打不开,报文件格式无效,怎么解决!
两个原因:
1.你的excel模版本身有问题,可以尝试新建一个模版。
2.你的excel使用了一些POI不支持的函数。
解决办法:
另存是由excel重写了完整的文件,可以解决问题。
关闭文件例子:
FileOutputStream os = new FileOutputStream("workbook.xls");
wb.write(os);
os.close();
在保护状态下execl的格式有可能正在被使用,你这边修改,准确说是线程冲突,一般excel值会作为导出文件的模板,是不会的。你可以在读的时候判断execl是否正在被使用。
下面的代码问题,你可以参考
package com.hwt.glmf.common;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
***隐藏网址***
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
/**
* 导出Excel公共方法
* @version 1.0
*
* @author wangcp
*
*/
public class ExportExcel extends BaseAction {
//显示的导出表的标题
private String title;
//导出表的列名
private String rowName ;
private List《Object》();
HttpServletResponse response;
//构造方法,传入要导出的数据
public ExportExcel(String title,String》 dataList){
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}
/*
* 导出数据
* */
public void export() throws Exception{
try{
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
HSSFSheet sheet = workbook.createSheet(title); // 创建工作表
// 产生表格标题行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);
//sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);
// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行)
// 将列头设置到sheet的单元格中
for(int n=0;n《columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(rowName);
cellRowName.setCellValue(text); //设置列头单元格的值
cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式
}
发现在easyui datagrid getSelections一个小bug,怎么解决
当使用批量删除的时候 会出现这种问题,明明已经删除了的那条数据,在后台跟踪代码的时候会发现id还是会传过去,原因就在于,选中多行进行提交时删除后,还有id还在缓存中;
解决办法在每次执行删除后,手动对获取到的行数据进行赋值,将其赋值为nul
easyui datagrid 获取不到值
你三个地方的productType不是同一个变量,所以你combobox的url中的值永远为undefined。
我做项目也遇到要js全局变量问题,但设置的全局变量,function中好像没法用,他会自己定义这个变量,所以你三个地方的productType不是你认为的同一个。
互相探讨,希望可以解决!
请高手指明:gridview的数据到导出Excel时,没有数据(为空白内容)
该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成。
下面是代码
县新建一个asp.ne的tweb应用程序把代码粘贴进去就好了
html页面代码
《%@ Page language="c#" Codebehind="OutExcel.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %》
《!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 》
《HTML》
《HEAD》
《title》OutPutExcel《/title》
《/HEAD》
《body》
《form id="Form1" method="post" runat="server"》
《asp:datagrid id="DataGrid1" runat="server"》
《Columns》
《asp:BoundColumn》《/asp:BoundColumn》
《/Columns》
《/asp:datagrid》
《P》
《asp:Label id="Label1" runat="server"》文件名:《/asp:Label》
《asp:TextBox id="TextBox1" runat="server"》《/asp:TextBox》
《asp:button id="Button1" runat="server" Text="输出到Excel"》《/asp:button》《/P》
《/form》
《/body》
《/HTML》
接下来是cs页面里的代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace eMeng.Exam
{
/// 《summary》
/// OutPutExcel 的摘要说明。
/// 《/summary》
public class OutPutExcel : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
private DataSet myDS =new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
Data_Load();//调用方法填充表格
}
}
/// 《summary》
/// 创建数据源
/// 《/summary》
/// 《returns》DataView《/returns》
private void Data_Load()
{
//数据库连接字符串Catalog为指定的数据库名称,DataSource为要连接的SQL服务器名称
string myConn ="User Id=sa;Password=sa;Initial Catalog=test;Data Source=zxb;Connect Timeout=20";
//查询字符串
string mySQLstr="SELECT * FROM fy";
//连接数据库操作
SqlConnection myConnection = new SqlConnection(myConn);
//执行SQL语句操作
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySQLstr,myConnection);
//打开数据库
myConnection.Open();
//向DataSet填充数据,填充数据库服务器中test库中的fy表
myDataAdapter.Fill(myDS,"fy");
//向DastaGrid填充数据
DataGrid1.DataSource=myDS;
DataGrid1.DataBind();
}
/// 《summary》
/// 输出到Excel
/// 《/summary》
/// 《param name="sender"》《/param》
/// 《param name="e"》《/param》
private void Button1_Click(object sender, System.EventArgs e)
{
if(TextBox1.Text=="")
{
Response.Write("《SCRIPT language=javascript》");
Response.Write("window.alert(’请输入文件名’);");
Response.Write("《/SCRIPT》");
}
else
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition","attachment;filename="+TextBox1.Text+".xls"); //定义输出文件和文件名
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// 《summary》
/// 设计器支持所需的方法 - 不要使用代码器修改
/// 此方法的内容。
/// 《/summary》
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells.Attributes.Add("style","vnd.ms-excel.numberformat:@");
e.Item.Cells.Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
}
还在继续研究别的方式
《%@ Page language="c#" Codebehind="OutExcel.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %》
《!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 》
《HTML》
《HEAD》
《title》OutPutExcel《/title》
《/HEAD》
《body》
《form id="Form1" method="post" runat="server"》
《asp:datagrid id="DataGrid1" runat="server"》
《Columns》
《asp:BoundColumn》《/asp:BoundColumn》
《/Columns》
《/asp:datagrid》
《P》
《asp:Label id="Label1" runat="server"》文件名:《/asp:Label》
《asp:TextBox id="TextBox1" runat="server"》《/asp:TextBox》
《asp:button id="Button1" runat="server" Text="输出到Excel"》《/asp:button》《/P》
《/form》
《/body》
《/HTML》
接下来是cs页面里的代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace eMeng.Exam
{
/// 《summary》
/// OutPutExcel 的摘要说明。
/// 《/summary》
public class OutPutExcel : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label1;
private DataSet myDS =new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
Data_Load();//调用方法填充表格
}
}
/// 《summary》
/// 创建数据源
/// 《/summary》
/// 《returns》DataView《/returns》
private void Data_Load()
{
//数据库连接字符串Catalog为指定的数据库名称,DataSource为要连接的SQL服务器名称
string myConn ="User Id=sa;Password=sa;Initial Catalog=test;Data Source=zxb;Connect Timeout=20";
//查询字符串
string mySQLstr="SELECT * FROM fy";
//连接数据库操作
SqlConnection myConnection = new SqlConne
夜_一少
excel公式选定的范围内有无效值怎么办
如果你用的是office2016或office365,那么它有新的函数可以使用averageif(),可以简单的达到答案;如果你用的office版本里没有那个函数那么可以自己写一个类似的,如=SUM(IF(ISNUMBER(A1:A20),A1:A20,0))/SUM(IF(ISNUMBER(A1:A20),1,0)),它是数组公式,结束时需要“三键齐按”(Ctrl+Shift+Enter)。

更多文章:
timeout on t2 timer什么意思(timer空调上什么意思)
2026年4月10日 18:15
jsp接收json文件(在jsp页面怎么接收json字符串,然后怎么把json字符串转换成对象,再之怎么遍历json对象)
2026年3月20日 12:00
思莱德品牌算大牌吗?思莱德所有中国零售店铺将于7月底关闭,该品牌为何坚持不下去了
2025年9月17日 23:15
extreme body mods(Extreme的歌曲歌词)
2026年2月13日 21:45
汇编语言和c语言哪个更高级(计算机中说的C语言是高级语言还是汇编语言)
2025年10月27日 03:45
java实现充值功能(我想用java自己写一个校园卡充值软件,需要掌握和用到哪些方面的东西求大神指教)
2026年8月20日 14:45
csdn手机网页弹窗(手机访问csdn弹出恶意广告如何解决)
2026年9月20日 00:00
个人主页设计思想(instagram个人主页怎么设计吸引人)
2025年5月25日 06:45
academic词性变化(学术,academic这个单词的名词是什么,不要academy)
2026年7月4日 17:00
m2u2课文翻译(谁能帮我翻译一下外研版英语初二下M2u2前两段的课文 急用谢谢!!!!1)
2026年9月19日 10:45
linkedin profile(LinkedIn Profile URL是什么意思)
2025年12月1日 15:45











