当前位置: 代码迷 >> ASP.NET >> 服务端gridview导出到excel的有关问题
  详细解决方案

服务端gridview导出到excel的有关问题

热度:9741   发布时间:2013-02-25 00:00:00.0
服务端gridview导出到excel的问题
Private Sub importExl()
  Dim fileName As String = Server.MapPath("\") & "//" & Session("user_id").ToString() & "_" & Me.lblEquType.Text.ToString().Trim() & "_draft.xlsx"
  Dim fs As New FileStream(fileName, FileMode.Create, FileAccess.Write)
  Dim streamW As New StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"))
  Dim sw As New StringWriter()
  GridConfigPara.RenderControl(htw)
  streamW.Write(sw.ToString())
  streamW.Flush()
  streamW.Close()
  End Sub
导出的excel打开错误,请教有什么好的方法 在服务端把gridview导出到excel。

------解决方案--------------------------------------------------------
http://www.cnblogs.com/hemood/archive/2009/08/25/1553643
------解决方案--------------------------------------------------------

C# code
  public static void PrintExcel(DataTable dt)        {            if (dt.Rows.Count > 0)            {                Excel.Application ExcelApp = new Excel.Application();                ExcelApp.Visible = true;                Excel.Workbooks workbooks = ExcelApp.Workbooks;                Excel._Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);                Excel.Sheets sheets = workbook.Worksheets;                Excel._Worksheet worksheet = (Excel._Worksheet)sheets.get_Item(1);                int intColumnsCount = dt.Columns.Count;                Excel.Range range_data = null;                int iRow = 5;                string strExcelBeginColumn = "A";                string strExcelEndColumn = string.Empty;                int a = intColumnsCount / 26;                if (a > 0)                {                    string strFirstKey = ((char)(65 + (a - 1))).ToString();                    int b = intColumnsCount % 26;                    string strLastKey = ((char)(65 + (b - 1))).ToString();                    if (b == 0)                    {                        strLastKey = "Z";                        if (strFirstKey == "A")                        {                            strFirstKey = string.Empty;                        }                        else                        {                            strFirstKey = ((char)(65 + (a - 2))).ToString();                        }                    }                    strExcelEndColumn = strFirstKey + strLastKey;                }                else                {                    strExcelEndColumn = ((char)(65 + (intColumnsCount - 1))).ToString();                }                                object[] objColumnsHeaderText = new object[intColumnsCount];                object[] objData = new object[intColumnsCount];                                for (int i = 0; i < dt.Columns.Count; i++)                {                    objColumnsHeaderText[i] = dt.Columns[i].ColumnName.ToString();                }                                range_data = worksheet.get_Range(strExcelBeginColumn + iRow.ToString().Trim(), strExcelEndColumn + iRow.ToString().Trim());                range_data.Font.Size = 10;                range_data.Value2 = objColumnsHeaderText;                range_data.Font.Bold = true;                range_data.Rows.HorizontalAlignment = HorizontalAlign.Center;                range_data.Borders.LineStyle = 1;                for (int i = 0; i < dt.Rows.Count; i++)                {                    iRow++;                    for (int j = 0; j < intColumnsCount; j++)                    {                        if (dt.Rows[i][j] != null)                        {                            objData[j] = dt.Rows[i][j].ToString().Trim();                        }                        else                        {                            objData[j] = "";                        }                    }                    range_data = worksheet.get_Range(strExcelBeginColumn + iRow.ToString().Trim(), strExcelEndColumn + iRow.ToString().Trim());                    range_data.Font.Size = 9;                    range_data.Value2 = objData;                    range_data.Borders.LineStyle = 1;                }            }        }
  相关解决方案