a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C# WinForm導出Excel方法介紹

2020-01-24 03:02:40
字體:
來源:轉載
供稿:網友

.NET開發人員首選的方法,通過COM組件調用Office軟件本身來實現文件的創建和讀寫,但是數據量較大的時候異常緩慢;如下代碼所示已經做了優化,將一個二維對象數組賦值到一個單元格區域中(下面的代碼中只能用于導出列數不多于26列的數據導出):

Office PIA

復制代碼 代碼如下:

public static void ExportToExcel(DataSet dataSet, string outputPath)
{
    Excel.ApplicationClass excel = new Excel.ApplicationClass();
    Excel.Workbook workbook = excel.Workbooks.Add(Type.Missing);
    int sheetIndex = 0;
    foreach (System.Data.DataTable dt in dataSet.Tables)
    {
        object[,] data = new object[dt.Rows.Count + 1, dt.Columns.Count];
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            data[0, j] = dt.Columns[j].ColumnName;
        }
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                data[i + 1, j] = dt.Rows[i][j];
            }
        }
        string finalColLetter = string.Empty;

        string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int colCharsetLen = colCharset.Length;
        if (dt.Columns.Count > colCharsetLen)
        {
            finalColLetter = colCharset.Substring(
                (dt.Columns.Count - 1) / colCharsetLen - 1, 1);
        }
        finalColLetter += colCharset.Substring(
                (dt.Columns.Count - 1) % colCharsetLen, 1);

        Excel.Worksheet sheet = (Excel.Worksheet)workbook.Sheets.Add(
            workbook.Sheets.get_Item(++sheetIndex),
            Type.Missing, 1, Excel.XlSheetType.xlWorksheet);
        sheet.Name = dt.TableName;
        string range = string.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1);
        sheet.get_Range(range, Type.Missing).Value2 = data;
        ((Excel.Range)sheet.Rows[1, Type.Missing]).Font.Bold = true;
    }
    workbook.SaveAs(outputPath, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    workbook.Close(true, Type.Missing, Type.Missing);
    workbook = null;
    excel.Quit();
    KillSpecialExcel(excel);
    excel = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
}

[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);

static void KillSpecialExcel(Excel.Application app)
{
    try
    {
        if (app != null)
        {
            int processId;
            GetWindowThreadProcessId(new IntPtr(app.Hwnd), out processId);
            System.Diagnostics.Process.GetProcessById(processId).Kill();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

文件流

這種方法的效率明顯高于第一種,而且也不需要安裝Office,但是導出的xls文件并不符合Excel的格式標準,在打開生成的xls文件時會提示:The file you are trying to open is in a different format that specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file.

復制代碼 代碼如下:

public static void ExportToExcel(System.Data.DataSet ds, string path)
{
    StreamWriter sw = null;
    try
    {
        long totalCount = ds.Tables[0].Rows.Count;
        sw = new StreamWriter(path, false, Encoding.Unicode);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
        {
            sb.Append(ds.Tables[0].Columns[i].ColumnName + "/t");
        }
        sb.Append(Environment.NewLine);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
            {
                sb.Append(ds.Tables[0].Rows[i][j].ToString() + "/t");
            }
            sb.Append(Environment.NewLine);
        }
        sw.Write(sb.ToString());
        sw.Flush();
    }
    catch (IOException ioe)
    {
        throw ioe;
    }
    finally
    {
        if (sw != null)
        {
            sw.Close();
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 好看的一级毛片 | 亚洲成人av在线 | 国产精品视频专区 | 日韩亚洲精品在线观看 | 国产美女福利 | 99pao成人国产永久免费视频 | 成人毛片在线视频 | 久久亚洲精品中文字幕蜜潮电影 | 日韩欧美不卡 | 天天干 夜夜操 | 欧美三级电影在线观看 | 久久久爽爽爽美女图片 | 黄色地址 | 国产亚洲精品久久久久动 | 成人在线视频免费观看 | 久久久久亚洲 | 欧美 日韩 亚洲 一区 | 国产精品1 | 成人精品一区二区 | 色综合天天综合网国产成人网 | 国产精品成人在线观看 | 91在线看片 | 国产成人在线播放 | 亚洲精品中文字幕中文字幕 | 亚洲一区二区免费在线观看 | 日日综合 | 国产视频三区 | 日本精品在线观看 | 在线国v免费看 | 国产精品1区2区 | 在线看av的网址 | 日韩不卡一区 | 男人的天堂免费 | 久久亚洲视频 | 久久国产精品无码网站 | 91免费看| 日本一区二区不卡 | 中文字幕av一区二区 | 欧美九九九| 亚洲精品www | 中文字幕第六页 |