You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.5 KiB
C#

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 Microsoft.Office.Interop.Excel;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace SopShow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//在窗体的OnLoad事件中调用该方法
//protected void Form1_OnLoad(...) {
// showOnMonitor(1);//index=1
//}
private void showOnMonitor(int showOnMonitor)
{
Screen[] sc;
sc = Screen.AllScreens;
if (showOnMonitor >= sc.Length) {
showOnMonitor = 0;
}
this.StartPosition = FormStartPosition.Manual;
this.Location = new System.Drawing.Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top);
// If you intend the form to be maximized, change it to normal then maximized.
this.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Maximized;
}
private void button1_Click(object sender, EventArgs e)
{
int showOnMonitor = 1;
Screen[] sc;
sc = Screen.AllScreens;
if (showOnMonitor >= sc.Length)
{
showOnMonitor = 0;
}
ShowExcel(sc[showOnMonitor]);
}
private void ShowExcel(Screen sc)
{
//IWorkbook workbook;
//FileStream fileStream = new FileStream(@"D:\test.xlsx", FileMode.Open, FileAccess.Read);
//workbook = new XSSFWorkbook(fileStream);
//NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(new FileStream("D:\\test.xlsx", FileMode.Open));
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//不可或缺
excelApp.Visible = true;
excelApp.DisplayFullScreen = true;
string top = excelApp.Top.ToString();
string Left = excelApp.Left.ToString();
Workbook wb = excelApp.Workbooks.Open("D:/test.xlsx", Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
try
{
Worksheet ws = (Worksheet)wb.Worksheets[1];
ws.ShowAllData();
GC.Collect();
GC.WaitForPendingFinalizers();
wb.Close(false, Type.Missing, Type.Missing);
excelApp.Quit();
}
catch (Exception e)
{
int i = 0;
}
finally
{
//GC.Collect();
//GC.WaitForPendingFinalizers();
//wb.Close(false, Type.Missing, Type.Missing);
//excelApp.Quit();
}
}
private void Form1_Load(object sender, EventArgs e)
{
//showOnMonitor(1);
//ShowExcel();
}
}
}