导出EXCEL的显示格式 我想在导出的EXCEL中如22.669988563这种涉及金额的数据中在EXCEL上面显示时只显示4位小数,但计算和选中时在EXCEL上面的详细栏中还是显示原来长度的小数位数,这个怎么做?第二个问题是在EXCEL中当A1=2时,可以设置A2=A1*3;也就是在另一个中是公式,在导出的EXCEL中能否也保存对应的公式呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 整个导出到excel应该是导出全部内容,只需要部分内容的话,恐怕只能每个内容截取了,实现方法从整体导出上去扩展excel上面的公式是在excel打开时通过自身的工具输入的,在导出时直接让excel的 数据带上需要的公式,这个难哦! 不是只要部分内容,是显示是只显示部分内容,但计算和在EXCEL的详细栏中还是要用全部内容; 设置单元格格式 mySheet.Cells(1,9).NumberFormatLocal = "@" Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]); r.NumberFormat = "@"; r.NumberForma = "0.00_ " get_Range函数是你自定义的吧? 给你一段代码研究下,就明白了GridView.AllowPaging = false; GridView.Width = 1400; GridView.Columns[4].Visible = true; GridView.Columns[6].Visible = true; GridView.Columns[7].Visible = true; GridView.Columns[8].Visible = true; GridView.Columns[5].Visible = false; GridView.HeaderStyle.Height = 50; GridView.Columns[1].ItemStyle.Width = 250; GridView.Columns[6].ItemStyle.Width = 550; GridView.Columns[2].ItemStyle.Width = 100; GridView.Columns[3].ItemStyle.Width = 100; GridView.Columns[4].ItemStyle.Width = 100; GridView.Columns[7].ItemStyle.Width = 100; GridView.Columns[8].ItemStyle.Width = 100; GridView.RowStyle.Height = 100; dataBind(); Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("广告播放历史数据", System.Text.Encoding.UTF8) + ".xls"); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! Response.ContentEncoding = System.Text.Encoding.UTF7; Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End(); 没有说是Gridview,是导出EXCEL,将一张表导到EXCEL 能保存相对应的公式不过这个要导入excel相对应office组件这样office能做什么事情,你程序都能做 要独立思考!呵呵!用我这个试试吧!// 当前对话 System.Web.HttpContext curContext = System.Web.HttpContext.Current; // IO用于导出并返回excel文件 System.IO.StringWriter strWriter = null; System.Web.UI.HtmlTextWriter htmlWriter = null; if (dgData != null) { // 设置编码和附件格式 curContext.Response.ContentType = "application/vnd.ms-excel"; //curContext.Response.ContentEncoding =System.Text.Encoding.UTF8; curContext.Response.Charset ="utf-8"; curContext.Response.AppendHeader("Content-Disposition","attachment;filename="+fname); // 导出excel文件 strWriter = new System.IO.StringWriter(); htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); for(int i = 0; i<dgData.Items.Count; i++) { for(int j = 0;j<dgData.Items[i].Cells.Count; j++) { dgData.Items[i].Cells[j].Attributes.Add("style","vnd.ms-excel.numberformat:@"); } } // 返回客户端 dgData.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); } 在Excel里用VBA来实现就好啦。 可以加“‘”就可以显示完全,也可以用r.NumberFormat = "@"; r.NumberForma = "0.00_ " 设置单元格格式 mySheet.Cells(1,9).NumberFormatLocal = "@" Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]); r.NumberFormat = "@"; r.NumberForma = "0.00_ " 他说的是对的,你要在项目中引用Microsoft.Office.Core和Microsoft.Office.Interop.Excel你要做的有这几步1.创建一个EXCELAplacation对象 _Application myExcel= new ApplicationClass(); myBook = myExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);2.创建一个Sheet其实就是EXCEL的一个页签啦 Worksheet curSheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets.Add(missing, missing, missing, missing);3.你不是要写值又要写公式么 就是用刚才人家给你的话 Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]); 的目的就是找到你要写值写公式的单元格 r.NumberFormat = "@"; r.NumberForma = "0.00_ " 那当然就是单元格的格式了 公式么肯定也有属性,你还是自己上CSDN查吧 不过教给你简单的一招,你自己在桌面上建个EXCEL,然后做设置公式的操作,用宏录制下来,然后看看宏利得代码是怎么写的,改改就行了4.释放EXCELAplacation资源,回收垃圾5.杀死EXCEL进程,第5绝对不可省略,因为第四步虽然连垃圾都回收了但是是不会杀死EXCEL进程的,如果你不杀死,最后的结果就是进程里全是EXCEL,最后机器越来越慢,直到崩溃 基于udp的即时通讯 谁有关于多线程操作数据库的例子...?求.. C#一个方法传值的问题 如何判断DateTimePicker的输入焦点在什么区域 【高分求解】如何得到一个有效的ICodeParser,或者如何通过代码解析为DOM 疑问刚解决,新疑问又来了,高手指导下。 使用GDI+画图,如何将图形中的某一部分保存为一个image? Winform 怎么打开IE 并 设置Cookie 来自动登录 请大神推荐一下你认为最有用的5个设计模式名称 谁有C#和ASP方面比较好的书籍 请介绍几本 谢谢 关于窗体关闭后,怎么令句柄为空 session问题
excel上面的公式是在excel打开时通过自身的工具输入的,在导出时直接让excel的 数据带上需要的公式,这个难哦!
mySheet.Cells(1,9).NumberFormatLocal = "@"
Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]);
r.NumberFormat = "@";
r.NumberForma = "0.00_ "
GridView.Width = 1400;
GridView.Columns[4].Visible = true;
GridView.Columns[6].Visible = true;
GridView.Columns[7].Visible = true;
GridView.Columns[8].Visible = true;
GridView.Columns[5].Visible = false;
GridView.HeaderStyle.Height = 50;
GridView.Columns[1].ItemStyle.Width = 250;
GridView.Columns[6].ItemStyle.Width = 550;
GridView.Columns[2].ItemStyle.Width = 100;
GridView.Columns[3].ItemStyle.Width = 100;
GridView.Columns[4].ItemStyle.Width = 100;
GridView.Columns[7].ItemStyle.Width = 100;
GridView.Columns[8].ItemStyle.Width = 100;
GridView.RowStyle.Height = 100;
dataBind();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("广告播放历史数据", System.Text.Encoding.UTF8) + ".xls"); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
能保存相对应的公式
不过这个要导入excel相对应office组件
这样office能做什么事情,你程序都能做
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (dgData != null)
{
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
//curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
curContext.Response.Charset ="utf-8";
curContext.Response.AppendHeader("Content-Disposition","attachment;filename="+fname);
// 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
for(int i = 0; i<dgData.Items.Count; i++)
{
for(int j = 0;j<dgData.Items[i].Cells.Count; j++)
{
dgData.Items[i].Cells[j].Attributes.Add("style","vnd.ms-excel.numberformat:@");
}
}
// 返回客户端
dgData.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
r.NumberFormat = "@";
r.NumberForma = "0.00_ "
mySheet.Cells(1,9).NumberFormatLocal = "@"
Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]);
r.NumberFormat = "@";
r.NumberForma = "0.00_ " 他说的是对的,你要在项目中引用Microsoft.Office.Core和Microsoft.Office.Interop.Excel
你要做的有这几步
1.创建一个EXCELAplacation对象
_Application myExcel= new ApplicationClass();
myBook = myExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
2.创建一个Sheet其实就是EXCEL的一个页签啦
Worksheet curSheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets.Add(missing, missing, missing, missing);
3.你不是要写值又要写公式么
就是用刚才人家给你的话
Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]);
的目的就是找到你要写值写公式的单元格
r.NumberFormat = "@";
r.NumberForma = "0.00_ "
那当然就是单元格的格式了
公式么肯定也有属性,你还是自己上CSDN查吧
不过教给你简单的一招,你自己在桌面上建个EXCEL,然后做设置公式的操作,用宏录制下来,然后看看宏利得代码是怎么写的,改改就行了
4.释放EXCELAplacation资源,回收垃圾
5.杀死EXCEL进程,第5绝对不可省略,因为第四步虽然连垃圾都回收了但是是不会杀死EXCEL进程的,如果你不杀死,最后的结果就是进程里全是EXCEL,最后机器越来越慢,直到崩溃