,我一般用Aspose.Cells.dll读写Excel数据
解决方案 »
- C# 如何将jpeg文件转换为PDF文件
- 惊爆:Graphics的bug。求证:有图有代码
- string变量赋值问题,一个我不能解释的问题要请教大家。
- 类前面加public 和不加publis有什么区别?
- windows服务与桌面交互的问题
- 没分了,各位帮个忙:DataGridView每一行行的高度能不能改呀
- 用过xtragrid的兄弟们帮忙看看这个!
- 在html 中,怎么实现"无文字也有下划线效果"
- 如何将FileOpenDialog嵌在form中,类似vs添加引用的浏览
- 怎么取得调用外部命令执行后的结果呢?比如DOS命令!
- 关于网络数据报文重组编程
- 采用wDoc.Paragraphs[i].Range.Text.Trim()语句读出来的字符串为何无法进行分割呢?
/// <summary>
/// 导入Excel
/// </summary>
/// <param name="Path"></param>
public static DataTable ReadExcel(string Path)
{
DataSet ds = new DataSet();
try
{
OleDbCommand oleCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", new OleDbConnection(EXCELConnString + Path));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
oleAdapter.Fill(ds, "[Sheet1$]");
}
catch (System.Exception)
{
return null;
}
// 数据绑定
return ds.Tables[0];
}返回一个DataTable,绑定到控件上。
1、 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码;导出之前需要关闭分页部分,若分页只导出首页的数据;
/// <summary>
/// 下载数据
/// </summary>
/// <param name="FileType">文件类型</param>
/// <param name="FileName">Excel表名</param>
private void Excel(string FileType, string FileName)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
//返回与指定代码页关联的数据
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//attachment表示作为附件下载,filename指定输出文件名称
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
//指定文件类型
Response.ContentType = FileType;
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
//定义一输入流
StringWriter tw = new StringWriter(myCItrad);
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.gvJstList.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
catch (Exception err)
{
s criptManager.RegisterStartups cript(this, this.GetType(), "js", "showInfo('ctl00_Contentplaceholder2_ValidationSummary1',1,'发生错误:" + err.Message.Replace("rn", "\r\n").Replace("'", "‘") + "')", true);
return;
}
}
如上代码如果处理一般的GridView导出应该是没有问题的,但是如果 GridView的 AutoGenerateDeleteButton,AutoGenerateEditButton,AutoGenerateSelectButton 有的设置为True了,或者GridView中有HyperLinkField类型的字段,那么就会抛出形如“类型“GridView”的控件 “XXXX”必须放在具有 runat=server 的窗体标记内。”的异常!
那么他的解决方案是:对WebForm窗体的VerifyRenderingInServerForm方法进行Override!
代码如下:public override void VerifyRenderingInServerForm(Control control)
{
//OverRide 为了使导出成Excel可行!
}
2、/* 如导出的表中有某些列为编号、身份证号之类的纯数字字符串,如不进行处理,则导出的数据会默认为数字,例如原字符串"0010"则会变为数字10,字符串"1245787888"则会变为科学计数法1.236+E9,这样便达不到我们想要的结果,所以需要在导出前对相应列添加格式化的数据类型,以下为格式化为字符串型*/在上面的代码中添加以下代码:foreach (GridViewRow dg in this.gridview1.Rows)
{
dg.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[5].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[6].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[8].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
} 或者是在DataGridView事件RowDataBound中添加以下代码:if(e.Row.RowType == DataControlRowType.DataRow )
{
e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
e.Row.Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}