我创建了一个服务器,多个客户端连接到该服务器,并将一些十六进制数据发送到服务器。数据由服务器处理,我想将这些数据保存在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文件中然后关闭它。
答案 0 :(得分: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网站上找到完整的样本。