zoukankan      html  css  js  c++  java
  • C#处理excel、pdf、access

    写PDF:

    写出 pdf 用到ICSharpCode.SharpZipLib.dll 、 itextsharp.dll (网上下)



     
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    //..
        
    //表格邊框寬度
        private float myTableBorderWidth = 0.01f;
        
    //使用字體
        private string myFontDir = @"C:\WINDOWS\Fonts\MINGLIU.TTC,0";
    //.
        BaseFont bfSun = BaseFont.createFont(this.myFontDir, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        iTextSharp.text.Font documentFont 
    = new iTextSharp.text.Font(bfSun,4,iTextSharp.text.Font.NORMAL);
        Document tmpDocument 
    = new Document(PageSize.A4.rotate());//横的
        tmpDocument.setMargins(6f,6f,20f,20f);//设置下边距
        PdfWriter pdfwriter = PdfWriter.getInstance(tmpDocument, new FileStream(this.outputdir+pdffilename + ".pdf", FileMode.Create));
        
    //设置页脚
        HeaderFooter footer = new HeaderFooter(new Phrase("Page ", documentFont), true);
        footer.Alignment 
    = Element.ALIGN_CENTER;
        footer.Border 
    = iTextSharp.text.Rectangle.NO_BORDER;
        tmpDocument.Footer 
    = footer; 
        tmpDocument.Open();
        Paragraph mytitle 
    = new Paragraph("标题123321\n\n", documentFont);
        mytitle.Alignment 
    = Element.ALIGN_CENTER;
        tmpDocument.Add(mytitle);
        
    //略过根据数据量判断是否分页的代码
        Table aTable = new Table( 24, (hasMultiPages?this.onepageContentNum:totalRowNum) + 1 );
        aTable.Border 
    = iTextSharp.text.Rectangle.LEFT | iTextSharp.text.Rectangle.BOTTOM;
        aTable.BorderWidth 
    = this.myTableBorderWidth;
        aTable.Widths 
    = new float[] {   12f, 12f, 15f, 18f, 12f, 12f, 12f, 15f, 18f, 12f,
            15f, 12f, 18f, 18f, 15f, 12f, 18f, 18f, 15f, 15f,
            15f, 15f, 12f, 12f};
        aTable.AutoFillEmptyCells 
    = true;//设置自动填充
        
    //设置表格中内容
        Cell tmpCell = new Cell(new Paragraph(tmpCellText+"\n", documentFont));
        tmpCell.HorizontalAlignment 
    = Element.ALIGN_CENTER;
        tmpCell.Border 
    = iTextSharp.text.Rectangle.TOP | iTextSharp.text.Rectangle.RIGHT;
        tmpCell.BorderWidth 
    = this.myTableBorderWidth;
        aTable.addCell(tmpCell, 
    new Point(i, pos));
        
    //.
        tmpDocument.Add(aTable);
        tmpDocument.newPage();
    //适当的时候调这句翻页
        
    //pdf打完收工
        tmpDocument.Add(aTable);
     

    读ACCESS:

        string strCon = "provider=microsoft.jet.oledb.4.0;" + @"data source=G:\abinxm\123.mdb";
        
    using (OleDbConnection con = new OleDbConnection(this.strCon))
        {
            con.Open();
            OleDbCommand cm 
    = new OleDbCommand(strSql, con);
            OleDbDataReader reader 
    = cm.ExecuteReader();
        
    while (reader.Read())
            {
            
    string strTmpIDCard = reader[0].ToString();
            
    //
        }
        
    //读完收工
        reader.Close();
        con.Close();

    读excel:
    引入Microsoft.Office.Interop.Excel.dll先 

        using Microsoft.Office.Interop.Excel;

        Microsoft.Office.Interop.Excel.Application ExcelObj 
    = new Microsoft.Office.Interop.Excel.Application();
        
    object missing = Type.Missing;
        Microsoft.Office.Interop.Excel.Workbook theWorkbook 
    = ExcelObj.Workbooks.Open(
             
    this.xlwfileurl, missing, missing, missing, missing, missing, missing, missing,
             missing, missing, missing, missing, missing, missing, missing);
        Microsoft.Office.Interop.Excel.Sheets sheets 
    = theWorkbook.Worksheets;
        Microsoft.Office.Interop.Excel.Worksheet datasheet 
    = null;
        
    foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in sheets) {
            
    if (sheet.Name == textBoxSheetName) { datasheet = sheet; break; }
        }
        
    if (null == datasheet)
        {
            MessageBox.Show(
    this"没有名称为" + textBoxSheetName + "的Sheet.");
            
    return;
        }
        
    int totalrow_num = datasheet.Rows.Count;
        
    //读死格式的文件,头不要了
        for (int i = 2; i <= totalrow_num; i++) {
            
    //读个值出来
        Object obj_id_card = datasheet.get_Range(datasheet.Cells[i, 3], datasheet.Cells[i, 3]).Value2 ;
        Microsoft.Office.Interop.Excel.Range range 
    = datasheet.get_Range(datasheet.Cells[i,2], datasheet.Cells[i, 25]);
        }
        
    //执行那么多释放资源,还是锁着读的那个excel文件呢~NND
        datasheet = null;
        sheets 
    = null;
        theWorkbook 
    = null;
        ExcelObj 
    = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
     


  • 相关阅读:
    模式识别 之 BP算法
    仪器开发 之 DICOM 三维重建 HPP
    模式识别 之 BP算法 (二)
    病理分析 之 细胞分析
    脉搏血氧仪 之 算法原理
    模式识别 之 初学
    机器学习 之 模糊神经(失败总结)
    冲刺第二天
    冲刺第三天
    团队项目计划
  • 原文地址:https://www.cnblogs.com/abinxm/p/1579839.html
Copyright © 2011-2022 走看看