2

我创建了一个服务器,多个客户端连接到该服务器,并将一些十六进制数据发送到服务器。数据由服务器处理,我想将此数据保存在 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();
    }
}
}

我想要的是我的服务器应该默默地创建名称为客户端IP地址的新excel文件,并在服务器处理完数据后向其中添加数据并静默保存。我该如何实现这一点,因为目前我的代码每次都会打开文件并将数据保存在 excel 文件中,然后将其关闭。

4

1 回答 1

2

您可以使用EPPlus。它比 Interop 更免费且更高效。

如果文件存在,则删除该文件,然后创建新文件,然后做任何你想做的事情。

FileInfo newFile = new FileInfo(fileName);
if (newFile.Exists)
 File.Delete(fileName);
ExcelPackage pck = new ExcelPackage(newFile);
..... //work with worksheets
pck.Save();

您可以在 EPPlus 网站上找到完整的示例。

于 2013-08-23T09:10:37.670 回答