寫PDF:
寫出 pdf 用到ICSharpCode.SharpZipLib.dll 、 itextsharp.dll (網上下)
Code
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:Code
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先 Code
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();