using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Net.Sockets;
using System.Data.OleDb;
using System.Globalization;
using System.IO;
using System.Reflection;
namespace excelBigData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void bt_Anlayz_Click(object sender, EventArgs e)
{
var appExcel = new Excel.Application();
object Nothing = System.Reflection.Missing.Value;
string stResultFileName = "D:" + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss");
int iCurrentLines = 0;
int iExcelCurrnetLines = 1;
appExcel.Visible = false;
Excel.Workbook workBook = appExcel.Workbooks.Add(Nothing);
Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];
worksheet.Name = "1";
string path = "D://2.txt";
//string headerLine = File.ReadLines(path).FirstOrDefault(); // Read the first row for headings
System.Text.Encoding _encoding = Encoding.GetEncoding("gb2312");//Encoding.UTF8; //Encoding.ASCII;
while (true)
{
var lines = File.ReadLines(path, _encoding).Skip(iCurrentLines).Take(10000);
var iter = lines.GetEnumerator();
if (iter == null)
{
break;
}
int size = lines.Count
if (size == 0)
{
break;
}
iCurrentLines += size;
//左上角和右下角的单元格A+iCurrentLines-size+1, 和Y+iCurrentLines
string strZuo = "A" + (iCurrentLines - size + 1);
string strYou = "Y" + (iCurrentLines);
object[,] datas = worksheet.Range[strZuo, strYou].Value2;
iExcelCurrnetLines = 1;
while (iter.MoveNext())
{
string dataStr = iter.Current;
string[] tempArr;
tempArr = dataStr.Split('\t');
for (int k = 1; k <= tempArr.Length; k++)
{
//worksheet.Cells[iExcelCurrnetLines, k] = tempArr[k - 1];
datas[iExcelCurrnetLines, k] = tempArr[k - 1];
/*
object[,] datas = worksheet.Range["c22", "f23"].Value2; //注意,excel vb 返回的二维数组的下标是从 1 开始的
datas[1, 1] = new Random().Next();
datas[2, 4] = DateTime.Now.ToShortTimeString();
sht.Range["c22", "f23"].Value2 = datas;
var endCell = sht.Range["c22"].End[excel.XlDirection.xlDown];*/
}
iExcelCurrnetLines++;
//fun(dataStr);
}
worksheet.Range[strZuo, strYou].Value2 = datas;
tbSentence.Text += "处理 " + iCurrentLines + " 条数据完成--------------------------\r\n";
}
/*
string path = "D://2.txt";
StreamReader sr = new StreamReader(path, Encoding.Default);
string strLine = sr.ReadLine();
int rowNum = 1;
object missing = System.Reflection.Missing.Value;
while (!string.IsNullOrEmpty(strLine))
{
string[] tempArr;
tempArr = strLine.Split('\t');
for (int k = 1; k <= tempArr.Length; k++)
{
worksheet.Cells[rowNum, k] = tempArr[k - 1];
}
strLine = sr.ReadLine();
rowNum++;
if (rowNum % 1000 == 0)
{
tbSentence.Text += "已处理 "+ rowNum + " 条数据 \r\n";
}
}
*/
worksheet.SaveAs(stResultFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
workBook.Close(false, Type.Missing, Type.Missing);
appExcel.DisplayAlerts = false;
/*Excel.Range excelRange = worksheet.get_Range(_workSheet.Cells[2, 2], _workSheet.Cells[4, 6]);
excelRange.EntireColumn.AutoFit();*/
appExcel.Quit();
foreach (Process p in Process.GetProcessesByName("Excel"))
{
if (string.IsNullOrEmpty(p.MainWindowTitle))
{
p.Kill();
}
}
MessageBox.Show("处理完成");
return;
}
}
}