代码之家  ›  专栏  ›  技术社区  ›  prattom

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

  •  2
  • prattom  · 技术社区  · 13 年前

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

    1 回复  |  直到 13 年前
        1
  •  2
  •   Irfan    13 年前

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

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

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

    您可以在EPPlus网站上找到完整的样本。