c#静默创建excel工作表并保存数据

c#静默创建excel工作表并保存数据,c#,excel-2010,C#,Excel 2010,我创建了一个多个客户端连接的服务器,它们向服务器发送一些十六进制数据。数据由服务器处理,我想将此数据保存在excel工作表中。我正在使用下面的代码,但是这个代码每次都会打开excel文件,将数据写入其中并关闭它。此外,excel文件应该已经存在 public class CreateExcelDoc { private static Excel.Workbook workbook = null; private static Excel.Worksheet worksheet

我创建了一个多个客户端连接的服务器,它们向服务器发送一些十六进制数据。数据由服务器处理,我想将此数据保存在excel工作表中。我正在使用下面的代码,但是这个代码每次都会打开excel文件,将数据写入其中并关闭它。此外,excel文件应该已经存在

public class CreateExcelDoc
{

    private static Excel.Workbook workbook = null;

    private static Excel.Worksheet worksheet = null;
    private static Excel.Range workSheet_range = null;
    private static Excel.Application app = new Excel.Application();
private static Excel.Workbooks workbooks = app.Workbooks;
    public static void createDoc()
    {
        object misValue = System.Reflection.Missing.Value;
        try
        {
            workbook = workbooks.Open("C:\\Documents and Settings\\pratyush\\Desktop\\test.xlsx", misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);


            app.Visible = true;
            worksheet = (Excel.Worksheet)workbook.Sheets[1];


        }
        catch (Exception e)
        {
            Console.Write("Error");
        }
        finally
        {

        }
    }

    public static void createHeaders(int row, int col, string htext, string cell1, string cell2, int mergeColumns, string b, bool font, int size, string fcolor)
    {
        worksheet.Cells[row, col] = htext;
        workSheet_range = worksheet.get_Range(cell1, cell2);
        workSheet_range.Merge(mergeColumns);
        switch (b)
        {
            case "YELLOW":
                workSheet_range.Interior.Color = System.Drawing.Color.Yellow.ToArgb();
                break;
            case "GRAY":
                workSheet_range.Interior.Color = System.Drawing.Color.Gray.ToArgb();
                break;
            case "GAINSBORO":
                workSheet_range.Interior.Color = System.Drawing.Color.Gainsboro.ToArgb();
                break;
            case "Turquoise":
                workSheet_range.Interior.Color = System.Drawing.Color.Turquoise.ToArgb();
                break;
            case "PeachPuff":
                workSheet_range.Interior.Color = System.Drawing.Color.PeachPuff.ToArgb();
                break;
            default:
                //  workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
                break;

        }

        workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
        workSheet_range.Font.Bold = font;
        workSheet_range.ColumnWidth = size;
        if (fcolor.Equals(""))
        {
            workSheet_range.Font.Color = System.Drawing.Color.White.ToArgb();
        }
        else
        {
            workSheet_range.Font.Color = System.Drawing.Color.Black.ToArgb();
        }

    }
    public static void addData(int row, int col, string data, string cell1, string cell2, string format)
    {
        worksheet.Cells[row, col] = data;
        workSheet_range = worksheet.get_Range(cell1, cell2);
        workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
        workSheet_range.NumberFormat = format;
    }

    public static void adddata()
    {
        createDoc();

        //creates the main header
        createHeaders(5, 2, "Total of Products", "B5", "D5", 2, "YELLOW", true, 10, "n");
        //creates subheaders
        createHeaders(6, 2, "Sold Product", "B6", "B6", 0, "GRAY", true, 10, "");


        //add Data to to cells
        addData(7, 2, "114287", "B7", "B7", "#,##0");


        workbook.Close(true, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
        app.Quit();


    }

    public static void Main()
    {
        adddata();
    }
}
}
我想要的是,我的服务器应该以静默方式创建新的excel文件,其名称为客户端ip地址,并在服务器处理后向其中添加数据,然后以静默方式保存。由于当前我的代码每次打开文件并将数据保存在excel文件中,然后将其关闭,因此如何实现这一点。

您可以使用。它比互操作更自由、更高效

如果文件存在,请删除它,然后创建新文件,并执行任何您想要的操作

FileInfo newFile = new FileInfo(fileName);
if (newFile.Exists)
 File.Delete(fileName);
ExcelPackage pck = new ExcelPackage(newFile);
..... //work with worksheets
pck.Save();
您可以在EPPlus网站上找到完整的样本