导出excel文件后获取错误:删除记录:来自/xl/worksheets/sheet1.xml部分的列信息

时间:2018-03-13 10:19:24

标签: c# winforms export-to-excel

我在很多论坛上都读过这个错误。但我无法解决我的问题。实际上导出我的文件后结果很好。但是在打开这样的文件时它会给出xml错误。 删除记录:来自/xl/worksheets/sheet1.xml部分的列信息

下面是我的代码块。

{
                double totalValue = 0;
                if (stock.Count > 0)
                {

                    ExcelPackage package = new ExcelPackage();


                    ExcelWorksheet excelWorksheet = package.Workbook.Worksheets.Add("StockList Report" + " - " + ((PictureBox)(sender)).Tag.ToString());


                    HeaderOfExcel(excelWorksheet, ((PictureBox)(sender)).Tag.ToString());

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

                        excelWorksheet.Cells[i + 3, 1].Value = lst[i].Reference.ToString();
                        excelWorksheet.Cells[i + 3, 2].Value = lst[i].Description.ToString();
                        excelWorksheet.Cells[i + 3, 3].Value = lst[i].Stock;
                        excelWorksheet.Cells[i + 3, 4].Value = lst[i].MinimumStock;
                        excelWorksheet.Cells[i + 3, 5].Value = lst[i].MaximumStock;
                        if (GWSM.Properties.Settings.Default.LOGGED_USER.Role.Id == (int)Roles.WarehouseUser)
                        {

                        }
                        else
                        {
                            excelWorksheet.Cells[i + 3, 6].Value = lst[i].Price;
                            excelWorksheet.Cells[i + 3, 6].Style.Numberformat.Format = "0.00000";
                            excelWorksheet.Cells[i + 3, 7].Value = lst[i].Value;
                            excelWorksheet.Cells[i + 3, 7].Style.Numberformat.Format = "0.00000";
                            totalValue += lst[i].Value;
                        }
                        excelWorksheet.Cells[i + 3, 8].Value = lst[i].Location.ToString() ;


                    }
                    if (Properties.Settings.Default.LOGGED_USER.Role.Id == (int)Roles.WarehouseUser)
                    {
                    }

                    else
                    {
                        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;


                        }

                    }

                    this.Cursor = Cursors.Default;
                    clsMethods.WriteToFile(package);

                }                

            }

以下是另一种方法。

static internal void WriteToFile(ExcelPackage package)
        {
            try
            {
                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();  // ensures we create a new workbook
                    newFile = new FileInfo(path);
                }

                FileStream file = new FileStream(path, FileMode.Create);
                package.SaveAs(file);
                file.Close(); 

            }
            catch (Exception)
            {

            }
        }

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

谢谢&amp;的问候,

0 个答案:

没有答案