解决方案 »
- 急。。。。。。在线等。。。。接收邮件登陆的问题。。。。全代码。。
- c#编程,挺好玩的
- C#的一个问题 实在让人费解
- 请问MDJM们怎么在javascript中正确传入单引号(')和斜杆(\或/),并且alert出来?
- 如何把用html 的EXCEL 文件保存成 真正的excel文件
- 一个window.location.href的问题
- 如何屏蔽IE工具栏及菜单??
- 当DropDownList得到焦点的时候,能不能触发它的点击事件,自动下拉?
- 很有意思的字符串问题
- !int.TryParse() 有问题。
- 请教高手:关于两个类的实例相等的判定
- repeater中td里面的linkbutton内容过长,如何设置可以自动换行
可以呀,一个excel,分为多个sheet页,先了解下基本知识。
好歹先设计好想要保存成什么格式,然后搜一搜如何用程序去操作对应类型的文件,最后用代码实现排版就行了
遇到问题学会分解,不要眉毛胡子一把抓
可以呀,一个excel,分为多个sheet页,先了解下基本知识。
你好我现在有用excel 我是winform项目 用的 C# using nopi; 有错误 这是我用的excel
//创建Application对象
excelApplication = new Excel.ApplicationClass(); // 打开已有的 excel 名字
excelWorkbooks = excelApplication.Workbooks;//工作薄实例声明
excelWorkbook = excelWorkbooks.Open(PAT3, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing) as Excel.Workbook; // excel.Application.Workbooks.Add(true); //不存在相同文件,则建立一个新的文件
//读取数据,通过Range对象
//Microsoft.Office.Interop.Excel.Range rng1 = excelWorksheet.get_Range("A1", Type.Missing);
//Console.WriteLine(rng1.Value2);
excelWorksheet = excelWorkbook.Worksheets[5] as Excel.Worksheet; //设置第几个表 1开始的
excelApplication.Visible = false;
//读取,通过Range对象,但使用不同的接口得到Range
for (num1 =0;num1<=119;num1++)
{
excelWorksheet.Cells[num1 + 1, 1] = Convert.ToSingle(strA[num1]) * U2C[num1, 0] * 1000; //PF-->fF excelWorksheet.Cells[num1 + 1, 2] = Convert.ToSingle(strB[num1]) * U2C[num1, 0] * 1000; //PF-->fF
}
excelWorksheet.Cells[num1 + 1, 1] = "空管标定电容值A";
excelWorksheet.Cells[num1 + 1, 2] = "空管标定电容值B";
excelWorksheet.Cells[num1 + 3, 1] = DateTime.Now.ToString();
excelWorksheet.Cells[num1 + 3, 2] = DateTime.Now.ToString(); try
{
excelWorkbook.Save();
excelWorksheet = null;
excelWorkbook = null;
excelWorkbooks = null;
// excelRange = null;
if (excelApplication != null)
{
excelApplication.Workbooks.Close();
excelApplication.Quit();
excelApplication = null;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
showtext5("空管导入excel中结束");
}
这里有下载,附带例子
//读取文本文件到Excel文件
private void btn_Read_Click(object sender, EventArgs e)
{
int P_int_Count=0;//记录正在读取的行数
string P_str_Line, P_str_Content = "";//记录读取行的内容及遍历到的内容
List<string> P_str_List = new List<string>();//存储读取的所有内容
StreamReader SReader = new StreamReader(txt_Txt.Text, Encoding.Default);//实例化流读取对象
while ((P_str_Line = SReader.ReadLine()) != null)//循环读取文本文件中的每一行
{
P_str_List.Add(P_str_Line);//将读取到的行内容添加到泛型集合中
P_int_Count++;//使当前读取行数加1
}
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
//打开指定的Excel文件
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Open(txt_Excel.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Microsoft.Office.Interop.Excel.Worksheet newWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(missing, missing, missing, missing);
excel.Application.DisplayAlerts = false;//不显示提示对话框
for (int i = 0; i < P_str_List.Count; i++)//遍历泛型集合
{
P_str_Content = P_str_List[i];//记录遍历到的值
if (Regex.IsMatch(P_str_Content, "^[0-9]*[1-9][0-9]*$"))//判断是否是数字
newWorksheet.Cells[i + 1, 1] = Convert.ToDecimal(P_str_Content).ToString("¥00.00");//格式化为货币格式,再添加到工作表中
else
newWorksheet.Cells[i + 1, 1] = P_str_Content;//直接将遍历到的内容添加到工作表中
}//codego.net/tags/1/1/
workbook.Save();//保存工作表
workbook.Close(false, missing, missing);//关闭工作表
MessageBox.Show("已经将文本文件内容成功导入Excel工作表中!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//将Excel文件读取到文本文件
private void btn_Txt_Click(object sender, EventArgs e)
{
//连接Excel数据库
OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txt_Path.Text + ";Extended Properties=Excel 8.0");
olecon.Open();//打开数据库连接
OleDbDataAdapter oledbda = new OleDbDataAdapter("select * from [" + cbox_SheetName.Text + "$]", olecon);//从工作表中查询数据
DataSet myds = new DataSet();//实例化数据集对象
oledbda.Fill(myds);//填充数据集
StreamWriter SWriter = new StreamWriter(cbox_SheetName.Text + ".txt", false, Encoding.Default);//实例化写入流对象
string P_str_Content = "";//存储读取的内容
for (int i = 0; i < myds.Tables[0].Rows.Count; i++)//遍历数据集中表的行数
{
for (int j = 0; j < myds.Tables[0].Columns.Count; j++)//遍历数据集中表的列数
{
P_str_Content += myds.Tables[0].Rows[i][j].ToString() + " ";//记录当前遍历到的内容
}
P_str_Content += Environment.NewLine;//字符串换行
}
SWriter.Write(P_str_Content);//先文本文件中写入内容
SWriter.Close();//关闭写入流对象
SWriter.Dispose();//释放写入流所占用的资源
MessageBox.Show("已经将" + cbox_SheetName.Text + "工作表中的数据成功写入到了文本文件中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
excelWorkbooks = excelApplication.Workbooks;//工作薄实例声明
excelWorkbook = excelWorkbooks.Open(PAT1 , Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing) as Excel.Workbook;
showtext5("开始保存" + "...");
excelWorksheet = excelWorkbook.Worksheets[10] as Excel.Worksheet; //设置第几个表 1开始的
excelApplication.Visible = false;
来读写就可以,而先前的数据也还在
不要给已有的单元格赋值不就不覆盖了