解决方案 »
- IE8 锚链接,IE offsetHeight 兼容问题(请各位前台高人帮忙)
- GRIDVIEW 绑定大量数据时异常缓慢.求助
- ???发挥你的想象力,测试一个大的网站方案
- 时间间隔判断
- 在GRIDVIEW里,如何把鼠标放在一行上,鼠标旁边的小字显示的是数据库中的内容?
- 怎么在页面中改变ascx用户控件里的标签控件的属性值?
- asp.net程序中,如何把在线帮助文档挂上?类似于桌面程序中的帮助菜单
- 求助SQL语句
- 新手求指点,感谢!在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的
- session赋值为null,是否表示已经完全删除session?
- easyui程序执行时提示缺少对象是怎么回事?(黄色部分)
- 关于ASP.NET MVC的理解问题
Error with Excel and C# when logged off the server
http://stackoverflow.com/questions/9075291/error-with-excel-and-c-sharp-when-logged-off-the-server
谢谢,看了你给的链接,问题描述的和我的是一样的,以下是我实现导出EXCEL的代码,请帮忙看一下,要怎么修改。
Imports Microsoft.VisualBasic
Imports System.Data
Imports Microsoft.Office.Interop.Excel
Imports excel = Microsoft.Office.Interop.ExcelPublic Class ExportExcel Private mDs As DataSet = New DataSet()
Private mFilePath As String = "" Public Sub New(ByRef ds As DataSet, ByVal FilePath As String) mDs = ds
mFilePath = FilePath
End Sub Public Function GetFilepath() As String Return mFilePath End Function Public Function DsToSheet(ByRef Msg As String) As Boolean Dim excel As excel.Application = New excel.Application()
excel.Visible = False
Dim ms As Object = Type.Missing
Dim wk As excel.Workbook = excel.Workbooks.Add(ms)
Dim ws As excel.Worksheet = DirectCast(wk.Worksheets(1), excel.Worksheet) Try For i = 0 To mDs.Tables(0).Columns.Count - 1 ws.Cells(1, i + 1) = mDs.Tables(0).Columns(i).ColumnName Next For i = 0 To mDs.Tables(0).Rows.Count - 1 For j = 0 To mDs.Tables(0).Columns.Count - 1 ws.Cells(i + 2, j + 1) = mDs.Tables(0).Rows(i)(j).ToString()
Next
Next wk.SaveAs(mFilePath)
wk.Close() Return True Catch ex As Exception Msg = "导出Excel文件失败," + ex.Message.ToString() Return False End Try excel.Quit() End FunctionEnd Class
//输出文件格式和文件名
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "商品信息表.xls");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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();
}
//字符串处理
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");
}
}
http://blog.163.com/ysh_good/blog/static/3731475020110135235766/
End If End Sub以下是绑定GridView代码:Protected Function DataBindGrideView() As DataSet Dim Conn As SqlConnection = New SqlConnection(Application("ConnStr"))
Dim FieldsStr As String = "CaseID, CaseName, AppName, PhoneNumber, WorkPlace, ProgressRate, TreatingMethod, TreatEndTime, TreatStarTime, ResponsiblePerson, AppTime, Note, Comment, Assistant"
Dim Sqlstr As String = "select " + FieldsStr + " from CaseReport " Select Case DropDownList_Cause.SelectedValue Case "ResponsiblePerson"
Sqlstr = Sqlstr + " where ResponsiblePerson = '" + Txt_Query.Text + "'" Case "CaseID" Sqlstr = Sqlstr + " where CaseID = " + Txt_Query.Text
Case "AppTime" Dim DateReg As Regex = New Regex("^([1-2]\d{3}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1]))$|([1-2]\d{3}/(0?[1-9]|1[0-2])/(0?[1-9]|[1-2]\d|3[0-1]))$") If DateReg.IsMatch(Txt_Query.Text.ToString()) Then Sqlstr = Sqlstr + " where DateDiff(dd,AppTime,'" + Txt_Query.Text + "') = 0" Else Response.Write("<script>alert('请输入正确的日期格式,如 2014/06/30 或 2014-06-30 !')</script>") Txt_Query.Text = "" Return Nothing Exit Function End If
End Select Sqlstr = Sqlstr + " Order by CaseID" Try Conn.Open() Dim da As SqlDataAdapter = New SqlDataAdapter(Sqlstr, Conn)
Dim ds As DataSet = New DataSet() da.Fill(ds, "CaseReport") Return ds Conn.Close()
Conn.Dispose() Catch ex As Exception Response.Write("<script language=JavaScript> alert('数据邦定失败," + ex.Message + "!');</script>") Return Nothing
End Try End Function
谢谢。在没有配置DCOM组件的时候,确实是因为权限的问题,但是后来我给NetwrokService开通启动和访问权限后,就不再报这个错误了,但是现在问题是有用户登录的时候不报错,一旦用户注销后,就开始报错,可能还真是是如3楼所说,需要启用GUI。