EXCEL读

            object missing = System.Reflection.Missing.Value;
            Application excel = new Application();//lauch excel application
            if (excel == null)
            {
                return list;
            }
            else
            {
                excel.Visible = false;
                excel.UserControl = true;
                // 以只读的形式打开EXCEL文件
                Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
                 missing, missing, missing, true, missing, missing, missing, missing, missing);
                //取得第一个工作薄

                string[] names = new string[wb.Worksheets.Count];
                for (int i = 0; i < wb.Worksheets.Count; i++)
                {
                    names[i] = ((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i + 1]).Name;
                    if (!sheetNames.Contains(names[i])) continue;

                    list.Add(new KeyValuePair<string, List<BatchTemplate1>>(names[i], new List<BatchTemplate1>()));
                    Worksheet ws = (Worksheet)wb.Worksheets.get_Item(i + 1);
                    //取得总记录行数   (包括标题列)
                    int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
                    //int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数

                    //取得数据范围区域 (不包括标题列) 
                    Range rng1 = ws.Cells.get_Range("A2", "C" + rowsint);   //item
                    object[,] arryItem = (object[,])rng1.Value2;   //get range's value
                    for (int j = 1; j < rowsint; j++)
                    {
                        if (arryItem[j, 1] == null) continue;
                        if (arryItem[j, 2] == null) continue;
                        list[names[i]].Add(new BatchTemplate1() { Number_OldName = arryItem[j, 1].ToString(), Name = arryItem[j, 2].ToString() });
                    }
                }
            }
            excel.Quit();
            excel = null;
            Process[] procs = Process.GetProcessesByName("excel");
            foreach (Process pro in procs)
            {
                pro.Kill();//没有更好的方法,只有杀掉进程
            }
            GC.Collect();

 

NPOI:

 

 

            IWorkbook workbook = null;
            string extension = System.IO.Path.GetExtension(strFileName);
            try
            {
                using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
                {
                    if (extension.Equals(".xls"))
                    {
                        workbook = new HSSFWorkbook(file);
                    }
                    else
                    {
                        workbook = new XSSFWorkbook(file);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }


            string[] names = new string[workbook.NumberOfSheets];
            
            for (int i = 0; i < workbook.NumberOfSheets; i++)
            {
                ISheet sheet = workbook.GetSheetAt(i);
                names[i] = sheet.SheetName;
                if (!sheetNames.Contains(names[i])) continue;

                list.Add(new KeyValuePair<string, List<BatchTemplate2>>(names[i], new List<BatchTemplate2>()));
                //取得总记录行数   (包括标题列)
                int rowsint = sheet.LastRowNum; //得到行数

                //取得数据范围区域 (不包括标题列)                 
                for (int j = 1; j < rowsint; j++)
                {
                    IRow row = sheet.GetRow(j);  //读取当前行数据
                    if (row != null)
                    {
                        if (row.GetCell(0) == null || row.GetCell(0).ToString().Length == 0) continue;
                        if (row.GetCell(1) == null || row.GetCell(1).ToString().Length == 0) continue;
                        if (row.GetCell(2) == null || row.GetCell(2).ToString().Length == 0) continue;
                        list[names[i]].Add(new BatchTemplate2()
                        {
                            Number_OldName = row.GetCell(0).ToString(),
                            X = row.GetCell(1).ToString(),
                            Y = row.GetCell(2).ToString(),
                            Z = row.GetCell(3).ToString(),
                        });
                    }
                }
                    
            }

 

 

NPOI链接

 

 

https://npoi.codeplex.com/releases

 

读取word2007:

 

            string fileName = @"E:\xxxx.docx";
            XWPFDocument document = null;
            try
            {
                using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    document = new XWPFDocument(file);
                }
            }
            catch (Exception ex)
            {
            }
            
            //foreach (XWPFHeader xwpfHeader in document.HeaderList)
            //{
            //}

            //foreach (XWPFFooter xwpfFooter in document.FooterList)
            //{
            //}

            
            foreach (XWPFTable table in document.Tables)
            {
                //循环表格行
                foreach (XWPFTableRow row in table.Rows)
                {
                    var c0 = row.GetCell(0);        //获得单元格0  
                    foreach (var para in c0.Paragraphs)
                    {
                        string text = para.ParagraphText;
                    }  
                    
                    //foreach (XWPFTableCell cell in row.GetTableCells())
                    //{
                    //    //
                    //}

                    row.GetCell(0).SetColor("#fbd4b4");
                }
            }

            //foreach (XWPFPictureData pictureData in document.AllPictures)
            //{
            //    string picExtName = pictureData.SuggestFileExtension();
            //    string picFileName = pictureData.FileName;
            //    byte[] picFileContent = pictureData.Data;
            //}

            //foreach (XWPFParagraph paragraph in document.Paragraphs)
            //{

            //}

测试写:

 

            XWPFDocument doc = new XWPFDocument();      //创建新的word文档

            XWPFParagraph p1 = doc.CreateParagraph();   //向新文档中添加段落
            p1.Alignment = ParagraphAlignment.CENTER; //段落对其方式为居中

            XWPFRun r1 = p1.CreateRun();                //向该段落中添加文字
            r1.SetText("测试段落一");

            XWPFParagraph p2 = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.LEFT;

            XWPFRun r2 = p2.CreateRun();
            r2.SetText("测试段落二");
            r2.FontSize = 16; //设置字体大小
            r2.IsBold = true; //设置粗体

            FileStream sw = File.Create(@"C:\Users\Administrator\Desktop\cutput.docx"); //...
            doc.Write(sw);                              //...
            sw.Close();  

新建写入EXCEL: 

IWorkbook workbook = null;

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.xls | *.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string newFilename = sfd.FileName;
                workbook = new HSSFWorkbook();
                //string[] names = new string[workbook.NumberOfSheets];

                ISheet sheet = workbook.CreateSheet();//.GetSheetAt(0);
                //names[0] = sheet.SheetName;

                for(int i=0;i<list.Count;i++)
                {
                    IRow row = sheet.CreateRow(i);//.GetRow(i);
                    NPOI.SS.UserModel.ICell cell = row.CreateCell(0);//.GetCell(0);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.String);
                    cell.SetCellValue(list[i].Cerno);
                }

                // 写入 
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                workbook.Write(ms);
                workbook = null;
                using (FileStream fs = new FileStream(newFilename, FileMode.Create, FileAccess.Write))
                {
                    byte[] data = ms.ToArray();
                    fs.Write(data, 0, data.Length);
                    fs.Flush();
                }
                ms.Close();
                ms.Dispose();

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵之章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值