using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.SS.UserModel;
using 大容量计量软件;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static HSSFWorkbook hssfworkbook;
static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
static void InitializeWorkbook()
{
//read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.
//book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added.
FileStream file = new FileStream(@"template/Book1.xls", FileMode.Open, FileAccess.Read);
hssfworkbook = new HSSFWorkbook(file);
//create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
hssfworkbook.DocumentSummaryInformation = dsi;
//create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
hssfworkbook.SummaryInformation = si;
}
private void button1_Click(object sender, EventArgs e)
{
InitializeWorkbook();
ISheet sheet1 = hssfworkbook.GetSheet("Sheet1");
//create cell on rows, since rows do already exist,it's not necessary to create rows again.
DataOperate dp = new DataOperate();
string s1 = "select * from tb_11111_01";
DataSet ds1 = new DataSet();
ds1 = dp.getDs(s1, "分米表");
DataTable dt = ds1.Tables["分米表"];
int n = dt.Rows.Count;
ICellStyle style1 = hssfworkbook.CreateCellStyle();
style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
IFont font1 = hssfworkbook.CreateFont();
font1.FontName = "Times New Roman";
font1.FontHeightInPoints = (short)14;
style1.SetFont(font1);
style1.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
style1.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
style1.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
style1.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
for (int i = 0; i < 30; i++)
{
IRow rr = sheet1.CreateRow(i + 2);
//rr.HeightInPoints = 50;
ICell cc0 = rr.CreateCell(0, CellType.STRING);
cc0.SetCellValue(dt.Rows[i][0].ToString());
//cc0.CellStyle = style1;
ICell cc1 = rr.CreateCell(1, CellType.STRING);
cc1.SetCellValue(dt.Rows[i][1].ToString());
//cc1.CellStyle = style1;
ICell cc2 = rr.CreateCell(2, CellType.STRING);
cc2.SetCellValue(dt.Rows[i][2].ToString());
//cc2.CellStyle = style1;
ICell cc3 = rr.CreateCell(3, CellType.STRING);
cc3.SetCellValue(dt.Rows[i][3].ToString());
//cc3.CellStyle = style1;
ICell cc4 = rr.CreateCell(4, CellType.STRING);
cc4.SetCellValue(dt.Rows[i][4].ToString());
//cc4.CellStyle = style1;
ICell cc5 = rr.CreateCell(5, CellType.STRING);
cc5.SetCellValue(dt.Rows[i][5].ToString());
//cc5.CellStyle = style1;
ICell cc6 = rr.CreateCell(6, CellType.STRING);
cc6.SetCellValue(dt.Rows[i][6].ToString());
//cc6.CellStyle = style1;
ICell cc7 = rr.CreateCell(7, CellType.STRING);
cc7.SetCellValue(dt.Rows[i][7].ToString());
//cc7.CellStyle = style1;
ICell cc8 = rr.CreateCell(8, CellType.STRING);
cc8.SetCellValue(dt.Rows[i][8].ToString());
//cc8.CellStyle = style1;
ICell cc9 = rr.CreateCell(9, CellType.STRING);
cc9.SetCellValue(dt.Rows[i][9].ToString());
//cc9.CellStyle = style1;
}
WriteToFile();
MessageBox.Show("dssf");
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.SS.UserModel;
using 大容量计量软件;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static HSSFWorkbook hssfworkbook;
static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
static void InitializeWorkbook()
{
//read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.
//book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added.
FileStream file = new FileStream(@"template/Book1.xls", FileMode.Open, FileAccess.Read);
hssfworkbook = new HSSFWorkbook(file);
//create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
hssfworkbook.DocumentSummaryInformation = dsi;
//create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
hssfworkbook.SummaryInformation = si;
}
private void button1_Click(object sender, EventArgs e)
{
InitializeWorkbook();
ISheet sheet1 = hssfworkbook.GetSheet("Sheet1");
//create cell on rows, since rows do already exist,it's not necessary to create rows again.
DataOperate dp = new DataOperate();
string s1 = "select * from tb_11111_01";
DataSet ds1 = new DataSet();
ds1 = dp.getDs(s1, "分米表");
DataTable dt = ds1.Tables["分米表"];
int n = dt.Rows.Count;
ICellStyle style1 = hssfworkbook.CreateCellStyle();
style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
IFont font1 = hssfworkbook.CreateFont();
font1.FontName = "Times New Roman";
font1.FontHeightInPoints = (short)14;
style1.SetFont(font1);
style1.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
style1.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
style1.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
style1.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
for (int i = 0; i < 30; i++)
{
IRow rr = sheet1.CreateRow(i + 2);
//rr.HeightInPoints = 50;
ICell cc0 = rr.CreateCell(0, CellType.STRING);
cc0.SetCellValue(dt.Rows[i][0].ToString());
//cc0.CellStyle = style1;
ICell cc1 = rr.CreateCell(1, CellType.STRING);
cc1.SetCellValue(dt.Rows[i][1].ToString());
//cc1.CellStyle = style1;
ICell cc2 = rr.CreateCell(2, CellType.STRING);
cc2.SetCellValue(dt.Rows[i][2].ToString());
//cc2.CellStyle = style1;
ICell cc3 = rr.CreateCell(3, CellType.STRING);
cc3.SetCellValue(dt.Rows[i][3].ToString());
//cc3.CellStyle = style1;
ICell cc4 = rr.CreateCell(4, CellType.STRING);
cc4.SetCellValue(dt.Rows[i][4].ToString());
//cc4.CellStyle = style1;
ICell cc5 = rr.CreateCell(5, CellType.STRING);
cc5.SetCellValue(dt.Rows[i][5].ToString());
//cc5.CellStyle = style1;
ICell cc6 = rr.CreateCell(6, CellType.STRING);
cc6.SetCellValue(dt.Rows[i][6].ToString());
//cc6.CellStyle = style1;
ICell cc7 = rr.CreateCell(7, CellType.STRING);
cc7.SetCellValue(dt.Rows[i][7].ToString());
//cc7.CellStyle = style1;
ICell cc8 = rr.CreateCell(8, CellType.STRING);
cc8.SetCellValue(dt.Rows[i][8].ToString());
//cc8.CellStyle = style1;
ICell cc9 = rr.CreateCell(9, CellType.STRING);
cc9.SetCellValue(dt.Rows[i][9].ToString());
//cc9.CellStyle = style1;
}
WriteToFile();
MessageBox.Show("dssf");
}
}
}
本文介绍如何使用NPOI库将SQL查询结果导出为Excel文件,包括初始化工作簿、读取模板、创建样式、设置单元格数据等步骤。
3469

被折叠的 条评论
为什么被折叠?



