C#静默创建Excel工作表并保存数据

标签 c# excel-2010

我创建了一个服务器,多个客户端连接到该服务器,并且它们向服务器发送一些十六进制数据。数据由服务器处理,我想将此数据保存在 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 文件中,然后关闭它。

最佳答案

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

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

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

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

关于C#静默创建Excel工作表并保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18398832/

相关文章:

c# - 原始 SQL 方法中的字符串插值,这怎么可能?

c# - 如何在unity3d中制作测距仪

excel - 如何在条件格式中定义自定义色阶?

Excel--SUMPRODUCTIF?变通?

c# - WPF 用户控件与自定义控件

c# - 如何在 C# 中使用本地数据库?

excel - 删除以某些字符开头的整个单词的公式

vba - 通过 VBA 将 XML 导入 EXCEL 不会刷新

c# - 对 streamwriter 和压缩如何工作的困惑

excel - 查找范围内的最后一行