zoukankan      html  css  js  c++  java
  • DataGridView内容导出到Excel文件

    1. public static void ExportToXlsFile(DataGridView dataGridView, string path)  
    2.         {  
    3.             XlsDocument doc = new XlsDocument();  
    4.             Workbook workbook = doc.Workbook;  
    5.             Worksheet worksheet = workbook.Worksheets.Add("sheet1");  
    6.             DataGridViewColumnCollection columns = dataGridView.Columns;  
    7.             foreach (DataGridViewColumn column in columns)  
    8.             {  
    9.                 ushort columnIndex = (ushort)column.DisplayIndex;  
    10.                 Cell cell = worksheet.Cells.Add((ushort)1, columnIndex + (ushort)1, column.HeaderText);  
    11.                 cell.Font.Bold = true;  
    12.                 ColumnInfo info = new ColumnInfo(doc, worksheet)  
    13.                 {  
    14.                     ColumnIndexStart = columnIndex,  
    15.                     ColumnIndexEnd = columnIndex,  
    16.                     Width = (ushort)(column.Width * 40),  
    17.                     Hidden = !column.Visible  
    18.                 };  
    19.                 worksheet.AddColumnInfo(info);  
    20.             }  
    21.   
    22.             int i = 2;  
    23.             foreach (DataGridViewRow row in dataGridView.Rows)  
    24.             {  
    25.                 foreach (DataGridViewCell cell in row.Cells)  
    26.                 {  
    27.                     object value = cell.FormattedValue;  
    28.                     worksheet.Cells.Add(i, cell.OwningColumn.DisplayIndex + 1, value).Font.Height = 160;  
    29.                 }  
    30.                 i++;  
    31.             }  
    32.             doc.FileName = path;  
    33.             doc.Save(true);  
    34.         }  
  • 相关阅读:
    《数据结构
    《数据结构
    《数据结构
    《算法
    《linux 进程管理》- ps/top/kill/killall/nice
    《linux 字符处理》- grep/sort/uniq/tr/paste/sed/awk
    《linux 文件目录》- touch/rm/mv/cat/head/tail/cp/mkdir/chmod/chown/find/locate/which/whereis/tar
    MySQL优化必须调整的10项配置
    PV-UV
    linux+nginx+mysql+php高性能服务器搭建
  • 原文地址:https://www.cnblogs.com/juefeiye/p/2117255.html
Copyright © 2011-2022 走看看