openxml追加写入已存在的xlsx文件

本文介绍了一种使用C#将Excel数据导入到DataTable的方法,并提供了详细的代码实现,包括如何处理不同类型的数据,如整数、浮点数、字符串和日期等。
        private void m2(string fname, string sheetName, DataTable dt, Func<DataTable, DataTable> dealer = null)
        {
            if (null != dealer)
                dt = dealer(dt);

            //string sheetName = "Data1";
            using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fname, true))
            {
                IEnumerable<Sheet> sheets = doc.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                    return;

                WorksheetPart wsPart = (WorksheetPart)doc.WorkbookPart.GetPartById(sheets.First().Id);
                Worksheet worksheet = wsPart.Worksheet;
                SheetData sheetData = worksheet.GetFirstChild<SheetData>();


                Row row = null;
                {
                    row = new Row();
                    Cell cell = null;
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        cell = new Cell();
                        cell.DataType = CellValues.String;
                        cell.CellValue = new CellValue(dt.Columns[i].ColumnName);
                        row.AppendChild(cell);
                    }
                    sheetData.AppendChild(row);

                }

                foreach (DataRow dr in dt.Rows)
                {
                    row = new Row();
                    Cell cell = null;
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        cell = new Cell();
                        cell.DataType = GetCellValueType(dr[i]);
                        cell.CellValue = new CellValue(dr[i].ToString());
                        row.AppendChild(cell);
                    }
                    sheetData.AppendChild(row);
                }

                worksheet.Save();
                doc.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
                doc.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
                doc.WorkbookPart.Workbook.Save();
                doc.Close();
            }
            //MyCommon.ExecuteProcess("explorer.exe", fname);
        }


        private CellValues GetCellValueType(object obj)
        {
            switch (obj.GetType().Name)
            {
                case "Int32":
                case "Int64":
                case "Decimal":
                case "Double":
                    return CellValues.Number;
                case "DateTime":
                case "String":
                    return CellValues.String;
                default:
                    return CellValues.String;
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值