导出excel文件时出现xml错误

时间:2018-03-19 11:03:54

标签: c# excel winforms

我有以下代码导出excel文件。文件正在导出正确的数据。但是当文件打开时,它会给出错误(删除记录:来自/xl/worksheets/sheet1.xml部分的列信息)。 我用Google搜索但无法解决问题。

以下是我的代码

ExcelPackage package = new ExcelPackage();
ExcelWorksheet excelWorksheet = package.Workbook.Worksheets.Add("Report" + " - " + ((PictureBox)(sender)).Tag.ToString());
HeaderOfExcel(excelWorksheet, ((PictureBox)(sender)).Tag.ToString());

                    int i;
                    for (i = 0; i < stk.Count; i++)
                    {

                        excelWorksheet.Cells[i + 3, 1].Value = stk[i].Rf.ToString();
                        excelWorksheet.Cells[i + 3, 2].Value = stk[i].Desc.ToString();
                        excelWorksheet.Cells[i + 3, 3].Value = stk[i].Stock;
                        excelWorksheet.Cells[i + 3, 4].Value = stk[i].MinStock;
                        excelWorksheet.Cells[i + 3, 5].Value = stk[i].Maxtock;
                        excelWorksheet.Cells[i + 3, 6].Value = stk[i].Price;
                        excelWorksheet.Cells[i + 3, 6].Style.Numberformat.Format = "0.00000";
                        excelWorksheet.Cells[i + 3, 7].Value = stk[i].Value;
                        excelWorksheet.Cells[i + 3, 7].Style.Numberformat.Format = "0.00000";
                        totalValue += stk[i].Value;                        
                        excelWorksheet.Cells[i + 3, 8].Value = stk[i].Location.ToString() ;

                        excelWorksheet.Cells[i + 3, 7].Style.Numberformat.Format = "0.00000";
                        excelWorksheet.Cells[i + 3, 7].Value = totalValue.ToString("n5") + " " + curr;
                        excelWorksheet.Cells[i + 3, 1].Value = "Total";


                        using (ExcelRange range = excelWorksheet.Cells["A" + (i + 3) + ":H" + (i + 3)])
                        {


                            range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                            range.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
                            range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;
                            range.Style.Font.Bold = true;

                        }
                        using (ExcelRange range = excelWorksheet.Cells[i + 3, 8])
                        {
                            range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Right;
                            range.Style.Font.Bold = true;


                        }

                    }


                    clsMethods.WriteToFile(package);

static internal void WriteToFile(ExcelPackage package)
    {
            string path = "";               
            SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
            SaveFileDialog1.Filter = "Excel files (*.xlsx)|*.xlsx";
            SaveFileDialog1.Title = "Save an Excel File";
            SaveFileDialog1.ShowDialog();
            path = SaveFileDialog1.FileName ;
            FileInfo newFile = new FileInfo(path);
            if (newFile.Exists)
            {
                newFile.Delete();
                newFile = new FileInfo(path);
            }
            FileStream file = new FileStream(path, FileMode.Create);
            package.SaveAs(file);
            file.Close();
    }

您的帮助可能会受到赞赏。

此致

0 个答案:

没有答案