2

Personpersoninfo.xlsと言う1つのワークシートでExcelファイルを作成したいと思います。

C#でそれを行う方法はありますか?

4

5 に答える 5

4

MyXLSでそれを行うことができます。

私はいくつかのプロジェクトでそれを使用して大きな成功を収めました。

そのオープンソース。

于 2009-10-07T05:52:01.337 に答える
2

このクラスを試してください:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;

namespace ExcelDemo
{
    static class ExcelImportExport
    {
        public static void ExportToExcel(string strFileName, string strSheetName)
        {
            // Run the garbage collector
            GC.Collect();

            // Delete the file if it already exists
            if (System.IO.File.Exists(strFileName))
            {
                System.IO.File.SetAttributes(strFileName, FileAttributes.Normal);
                System.IO.File.Delete(strFileName);
            }

            // Open an instance of excel. Create a new workbook.
            // A workbook by default has three sheets, so if you just want a single one, delete sheet 2 and 3
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Excel._Workbook xlWB = (Excel._Workbook)xlApp.Workbooks.Add(Missing.Value);
            Excel._Worksheet xlSheet = (Excel._Worksheet)xlWB.Sheets[1];
            ((Excel._Worksheet)xlWB.Sheets[2]).Delete();
            ((Excel._Worksheet)xlWB.Sheets[2]).Delete();

            xlSheet.Name = strSheetName;
            // Write a value into A1
            xlSheet.Cells[1, 1] = "Some value";

            // Tell Excel to save your spreadsheet
            xlWB.SaveAs(strFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            xlApp.Quit();

            // Release the COM object, set the Excel variables to Null, and tell the Garbage Collector to do its thing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

            xlSheet = null;
            xlWB = null;
            xlApp = null;

            GC.Collect();

        }


    }

}
于 2009-10-07T06:00:48.653 に答える
2

Excel 相互運用機能を試しましたか? これは Excel 2003/2007 用ですか? Excel 2007 xml の場合は、試すことができます

カルロス

スプレッドシートギア

ExcelPackage (コードプレックス)

于 2009-10-07T05:51:27.037 に答える
1

Excel 2007 以降との互換性だけが必要な場合は、OpenXML SDKを使用します。

非常に基本的な例については、このスレッドの最初のページの最後の投稿を確認してください。

SpreadsheetML は最初はかなり混乱する可能性がありますが、定期的にオフィス ドキュメントを生成する必要がある場合は、学習する価値があります。openxmldeveloper.orgでリソースを見つけることができます。

敬具
オリバー・ハナッピ

于 2009-10-15T09:35:49.467 に答える
0

SpreadsheetGear for .NETは、次のコードでそれを行います。

            // Create a new empty workbook with one worksheet.
            IWorkbook workbook = Factory.GetWorkbook();
            // Get the worksheet and change it's name to "Person".
            IWorksheet worksheet = workbook.Worksheets[0];
            worksheet.Name = "Person";
            // Put "Hello World!" into A1.
            worksheet.Cells["A1"].Value = "Hello World!";
            // Save the workbook as xls (Excel 97-2003 / Biff8).
            workbook.SaveAs(@"C:\tmp\personinfo.xls", FileFormat.Excel8);
            // Save the workbook as xlsx (Excel 2007 Open XML).
            workbook.SaveAs(@"C:\tmp\personinfo.xlsx", FileFormat.OpenXMLWorkbook);

自分で試してみたい場合は、ここから無料の試用版をダウンロードできます。

免責事項: 私は SpreadsheetGear LLC を所有しています

于 2009-10-07T15:42:05.557 に答える