示例#1
0
文件: write.cs 项目: Fun33/code
 private void BtnV1_Click(object sender, EventArgs e)
 {
     HSSF.UserModel.HSSFWorkbook wb   = V1();
     System.IO.FileStream        file = new System.IO.FileStream(@"D:\cadmen\una_work\sample\NPOI\0_v1.xls", System.IO.FileMode.Create); //1.產生檔案
     wb.Write(file);                                                                                                                     //2.put steam to file
     file.Close();                                                                                                                       //3.close file
 }
示例#2
0
文件: NPOI.cs 项目: Fun33/code
        //範例二,DataTable轉成Excel檔案的方法
        private void write(DataTable dt, string fileName)
        {
            try
            {
                //建立Excel 2003檔案
                SS.UserModel.IWorkbook wb = new HSSF.UserModel.HSSFWorkbook();
                SS.UserModel.ISheet    sheet;

                ////建立Excel 2007檔案
                //IWorkbook wb = new XSSFWorkbook();
                //ISheet ws;

                if (dt.TableName != string.Empty)
                {
                    sheet = wb.CreateSheet(dt.TableName);
                }
                else
                {
                    sheet = wb.CreateSheet("Sheet1");
                }

                sheet.CreateRow(0);//第一行為欄位名稱
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    sheet.GetRow(0).CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sheet.CreateRow(i + 1);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        sheet.GetRow(i + 1).CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
                    }
                }

                System.IO.FileStream file = new System.IO.FileStream(fileName, System.IO.FileMode.Create); //1.產生檔案
                wb.Write(file);                                                                            //2.put steam to file
                file.Close();                                                                              //3.close file
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }