dev的 Gridview 能不能实现Excel放大和缩小的功能?,让表格缩小能显示更多东西,如果不行的话有没有其他办法代替的?
gridheight
The height (in pixels) of the component in cells. Used with REMAINDER and gridwidth.
gridwidth
The width (in pixels) of the component in cells. Used with REMAINDER and gridheight.
private void excelBtn_Click(object sender, EventArgs e)
{
var dig = new OpenFileDialog()
{
Title = "请选择要导入的文件",
Filter = "Excel Files|*.xlsx;*.xls",
Multiselect = false//是否可以多选文件
};
//选择文件时取消
if (dig.ShowDialog()!=DialogResult.OK) {
return;
}
var inputFilename = dig.FileName;
//返回指定的路径字符串的扩展名,Extension扩展
var ext = Path.GetExtension(inputFilename);
// MessageBox.Show("ext:"+ext);
//定义工作簿
IWorkbook workbook;
//使用文件流,FileMode .Open打开现有文件
using (var stream = new FileStream(inputFilename, FileMode
.Open))
{
//扩展名判断
if (".xlsx".Equals(ext, StringComparison.OrdinalIgnoreCase))
{
//打开.xlsx文件
workbook = new XSSFWorkbook(stream);
}
else
{
//打开.xls文件
workbook = new HSSFWorkbook(stream);
}
//获取第一个表
var sheet= workbook.GetSheetAt(0);
//定义行号,从第二行开始读
int rowindex = 1;
//反射,获取student类的公共属性
var properties = typeof(Student).GetProperties(BindingFlags.Instance | BindingFlags.Public);
//定义一个泛型student列表
var dataRows = new List<Student>();
while (true) {
//获取行
var row = sheet.GetRow(rowindex);
if (row == null) {
break;
}
//实例对象
var rowData = new Student();
//定义列
int columIndex = 0;
foreach (var property in properties)
{
//遍历反射对象的属性,比如学号,班级,姓名
var cell = row.GetCell(columIndex);
property.SetValue(rowData, cell.ToString(), null);
columIndex++;
}
//添加到列表保存
dataRows.Add(rowData);
rowindex++;
}
//添加到GridView显示
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dataRows;
}
}
最后效果: